147 lines
4.5 KiB
Python
147 lines
4.5 KiB
Python
import re
|
|
|
|
opToBinOP = {
|
|
"ADD": 1,
|
|
"MUL": 2,
|
|
"SUB": 3,
|
|
"DIV_INT": 4,
|
|
"COP": 5,
|
|
"AFC": 6,
|
|
"JMP": 7,
|
|
"JMF": 8,
|
|
"INF": 9,
|
|
"SUP": 10,
|
|
"EQ": 11,
|
|
"PRI": 12,
|
|
"AND": 13,
|
|
"OR": 14,
|
|
"NOT": 15
|
|
}
|
|
|
|
|
|
def output(s):
|
|
fileOutput = open('asm2', 'w')
|
|
fileOutput.write("\n".join(s))
|
|
fileOutput.close()
|
|
|
|
|
|
def convertToRegister(s):
|
|
# TODO check if there is a /\d_label/ in s[0] and if so adds it back at the start of the output
|
|
l = []
|
|
|
|
if not re.match(r"\d_LABEL .*", s[0]) :
|
|
optionalFlag = ""
|
|
incr = 0
|
|
else:
|
|
optionalFlag = s[0] + " "
|
|
incr = 1
|
|
|
|
match s[0]:
|
|
case "ADD":
|
|
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("ADD R0 R0 R1")
|
|
l.append("STORE "+s[1+incr]+" R0")
|
|
case "MUL":
|
|
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("MUL R0 R0 R1")
|
|
l.append("STORE "+s[1+incr]+" R0")
|
|
case "SUB":
|
|
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("SUB R0 R0 R1")
|
|
l.append("STORE "+s[1+incr]+" R0")
|
|
case "DIV_INT":
|
|
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("DIV R0 R0 R1")
|
|
l.append("STORE "+s[1+incr]+" R0")
|
|
case "COP":
|
|
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
|
l.append("STORE "+s[1+incr]+" R0")
|
|
case "AFC":
|
|
l.append(optionalFlag+"AFC R0 "+s[2+incr])
|
|
l.append("STORE "+s[1+incr]+" R0")
|
|
case "JMP":
|
|
pass
|
|
case "JMF":
|
|
pass
|
|
case "INF":
|
|
l.append("LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("INF R2 R0 R1")
|
|
case "SUP":
|
|
l.append("LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("SUP R2 R1 R0")
|
|
case "EQ":
|
|
l.append("LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("EQ R2 R1 R0")
|
|
case "PRI":
|
|
l.append("PRI "+s[2+incr])
|
|
case "AND":
|
|
l.append("LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("AND R2 R0 R1")
|
|
case "OR":
|
|
l.append("LOAD R0 "+s[2+incr])
|
|
l.append("LOAD R1 "+s[3+incr])
|
|
l.append("OR R2 R0 R1")
|
|
case "NOT":
|
|
l.append("LOAD R0 "+s[2+incr])
|
|
l.append("NOT R2 R0")
|
|
""" R2 contiendra la valeur qui dit s'il faut sauter ou non """
|
|
|
|
return l
|
|
|
|
|
|
totalLine = 0 # TODO Check the number of line is never reached
|
|
labelCount = 0 # used to create a new label each time
|
|
|
|
fileInput = open("asm", "r")
|
|
ASMLines = list(map(lambda e: e.rstrip("\n"), fileInput.readlines()))
|
|
fileInput.close()
|
|
|
|
ASMLinesLabel = ASMLines[:] # will contain at the end of the first loop the code with labels inserted
|
|
ASMLinesRegister = [] # will contain at the end of the 2nd loop the registry-based code with labels
|
|
ASMLinesFinal = [] # will contain the output, register-based, code
|
|
for i, l in enumerate(ASMLines):
|
|
items = l.split(" ")
|
|
if items[0] in ["JMP", "JMF"]:
|
|
lineToJumpTo = int(items[
|
|
1 if items[0] == "JMP" else 2
|
|
])
|
|
if re.match(r"\d_LABEL .*", ASMLinesLabel[lineToJumpTo]):
|
|
ASMLinesLabel[i] = " ".join(ASMLines[i].split()[:-1] + [ASMLinesLabel[lineToJumpTo].split()[0]])
|
|
else:
|
|
ASMLinesLabel[lineToJumpTo] = f"{labelCount}_LABEL " + ASMLines[lineToJumpTo]
|
|
ASMLinesLabel[i] = " ".join(ASMLinesLabel[i].split()[:-1] + [f"{labelCount}_LABEL"])
|
|
labelCount += 1
|
|
print(ASMLinesLabel)
|
|
|
|
for i, l in enumerate(ASMLinesLabel):
|
|
ASMLinesRegister.extend(convertToRegister(l.split()))
|
|
print(ASMLinesRegister)
|
|
|
|
labels = {}
|
|
for i, l in enumerate(ASMLinesRegister):
|
|
if re.match(r"\d_LABEL .*", l):
|
|
labels[l.split()[0]] = i
|
|
ASMLinesRegister[i] = " ".join(ASMLinesRegister[i].split()[1:])
|
|
print(ASMLinesRegister)
|
|
|
|
for i, l in enumerate(ASMLinesRegister):
|
|
label = re.match(r"\d_LABEL", l.split()[-1])
|
|
if label:
|
|
ASMLinesFinal.append(" ".join(l.split()[:-1] + [str(labels[label[0]])]))
|
|
else :
|
|
ASMLinesFinal.append(l)
|
|
|
|
|
|
print(ASMLinesRegister)
|
|
output(ASMLinesFinal)
|
|
# - trucs en registre
|
|
# - décaler les Jumps
|
|
# - COP -> OPCode
|