#!/usr/bin/env python

# Copyright 2005 Paul Wise <pabs3@bonedaddy.net>

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:

# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
	WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 
	                This code does less than half of what it should. Fixes welcome.
	           It is meant to be a converter for Intel to AT&T inline assembly syntax.
	WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 

	asm-converter -- Converts C source code using Intel inline assembly into AT&T assembly
	Usage: asmconverter < foo.c > bar.c ; gcc -o foo bar.c
"""

import sys
import re

asm_start = re.compile('[a-z{]');
az = re.compile('[a-z]');
asm_end = re.compile('[};]');
op_args = re.compile('([a-zA-Z]*) *([^,]*),*(.*)');

registers = [
	'eax',
	'ebx',
	'ebp',
	'esp',
	'edx',
	'edi',
	'esi',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	'',
	
]

old = sys.stdin
new = sys.stdout

data = old.read()
start = end = 0

while 1:
	begin = data.find('_asm',start)
	if begin == -1:
		break
	new.write(data[start:begin])
	new.write('asm(')
	asm = asm_start.search(data,begin+4)
	if asm == None:
		break
	asm = asm.start()
	end = asm_end.search(data,asm+1)
	if end == None:
		break
	end = end.start()
	#sys.stderr.write("start: %s begin: %s end: %s\n" % (start, begin, end))
	#sys.stderr.write("asm: %s begin: %s end: %s\n" % (data[asm], data[begin], data[end]))
	lines_start = asm;
	if data[asm]=='{':
		lines_start+=1
	lines = data[lines_start:end].split("\n")
	for line in lines:
		comment = line.find('//') # strip comments
		if comment != -1:
			line = line[0:comment]
		line = line.strip() #strip whitespace
		if line == '':
			continue
		match = op_args.match(line)
		groups = list(match.groups())
		groups[2] = groups[2].strip()
		if groups[1] == '':
			del groups[1]
		elif groups[1] in registers:
			groups[1] = '%'+groups[1]
		sys.stderr.write("%s\n"%groups)
		try:
			if groups[2] == '':
				del groups[2]
		except IndexError:
			pass
		if len(groups) == 1:
			line = groups[0]
		if len(groups) == 2:
			line = "%s %s" % ( groups[0], groups[1] )
		elif len(groups) == 3:
			line = "%s %s, %s" % ( groups[0], groups[2], groups[1] )
		else:
			raise "Wrong number of groups"
		new.write("\"%s\\n\"\n"%line)
	start = end+1
	new.write(');')

new.write( data[start:] )
