finished cross compiler for test
This commit is contained in:
parent
8f5be60008
commit
e621b754bf
1 changed files with 86 additions and 70 deletions
156
post-process.py
156
post-process.py
|
@ -1,98 +1,107 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
opToBinOP = {
|
opToBinOP = {
|
||||||
"ADD": 1,
|
"ADD": "01",
|
||||||
"MUL": 2,
|
"MUL": "02",
|
||||||
"SUB": 3,
|
"SUB": "03",
|
||||||
"DIV_INT": 4,
|
"DIV": "04",
|
||||||
"COP": 5,
|
"COP": "05",
|
||||||
"AFC": 6,
|
"AFC": "06",
|
||||||
"JMP": 7,
|
"LOAD": "07",
|
||||||
"JMF": 8,
|
"STORE": "08",
|
||||||
"INF": 9,
|
"INF": "09",
|
||||||
"SUP": 10,
|
"SUP": "0A",
|
||||||
"EQ": 11,
|
"EQ": "0B",
|
||||||
"PRI": 12,
|
"NOT": "0C",
|
||||||
"AND": 13,
|
"AND": "0D",
|
||||||
"OR": 14,
|
"OR": "0E",
|
||||||
"NOT": 15
|
"JMP": "0F",
|
||||||
|
"JMF": "10",
|
||||||
|
"CAL": "11",
|
||||||
|
"RET": "12",
|
||||||
|
"PRI": "13",
|
||||||
|
"NOP": "FF"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def output(s):
|
def output(s, num, oneline=False):
|
||||||
fileOutput = open('asm2', 'w')
|
fileOutput = open(f'asm{num}', 'w')
|
||||||
fileOutput.write("\n".join(s))
|
if oneline:
|
||||||
|
fileOutput.write(s)
|
||||||
|
else :
|
||||||
|
fileOutput.write("\n".join(s))
|
||||||
fileOutput.close()
|
fileOutput.close()
|
||||||
|
|
||||||
|
|
||||||
def convertToRegister(s):
|
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 = []
|
l = []
|
||||||
|
|
||||||
if not re.match(r"\d_LABEL .*", s[0]) :
|
if not re.match(r"\d_LABEL", s[0]):
|
||||||
optionalFlag = ""
|
optionalFlag = ""
|
||||||
incr = 0
|
incr = 0
|
||||||
|
op = s[0]
|
||||||
else:
|
else:
|
||||||
optionalFlag = s[0] + " "
|
optionalFlag = s[0] + " "
|
||||||
incr = 1
|
incr = 1
|
||||||
|
op = s[1]
|
||||||
|
|
||||||
match s[0]:
|
match op:
|
||||||
case "ADD":
|
case "ADD":
|
||||||
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("ADD R0 R0 R1")
|
l.append("ADD 0 0 1")
|
||||||
l.append("STORE "+s[1+incr]+" R0")
|
l.append("STORE " + s[1 + incr] + " 0")
|
||||||
case "MUL":
|
case "MUL":
|
||||||
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("MUL R0 R0 R1")
|
l.append("MUL 0 0 1")
|
||||||
l.append("STORE "+s[1+incr]+" R0")
|
l.append("STORE " + s[1 + incr] + " 0")
|
||||||
case "SUB":
|
case "SUB":
|
||||||
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("SUB R0 R0 R1")
|
l.append("SUB 0 0 1")
|
||||||
l.append("STORE "+s[1+incr]+" R0")
|
l.append("STORE " + s[1 + incr] + " 0")
|
||||||
case "DIV_INT":
|
case "DIV_INT":
|
||||||
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("DIV R0 R0 R1")
|
l.append("DIV 0 0 1")
|
||||||
l.append("STORE "+s[1+incr]+" R0")
|
l.append("STORE " + s[1 + incr] + " 0")
|
||||||
case "COP":
|
case "COP":
|
||||||
l.append(optionalFlag+"LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("STORE "+s[1+incr]+" R0")
|
l.append("STORE " + s[1 + incr] + " 0")
|
||||||
case "AFC":
|
case "AFC":
|
||||||
l.append(optionalFlag+"AFC R0 "+s[2+incr])
|
l.append(optionalFlag + "AFC 0 " + s[2 + incr])
|
||||||
l.append("STORE "+s[1+incr]+" R0")
|
l.append("STORE " + s[1 + incr] + " 0")
|
||||||
case "JMP":
|
case "JMP":
|
||||||
pass
|
l.append(" ".join(s))
|
||||||
case "JMF":
|
case "JMF":
|
||||||
pass
|
l.append(" ".join(s))
|
||||||
case "INF":
|
case "INF":
|
||||||
l.append("LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("INF R2 R0 R1")
|
l.append("INF 2 0 1")
|
||||||
case "SUP":
|
case "SUP":
|
||||||
l.append("LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("SUP R2 R1 R0")
|
l.append("SUP 2 1 0")
|
||||||
case "EQ":
|
case "EQ":
|
||||||
l.append("LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("EQ R2 R1 R0")
|
l.append("EQ 2 1 0")
|
||||||
case "PRI":
|
case "PRI":
|
||||||
l.append("PRI "+s[2+incr])
|
l.append(optionalFlag + "PRI " + s[2 + incr])
|
||||||
case "AND":
|
case "AND":
|
||||||
l.append("LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("AND R2 R0 R1")
|
l.append("AND 2 0 1")
|
||||||
case "OR":
|
case "OR":
|
||||||
l.append("LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("LOAD R1 "+s[3+incr])
|
l.append("LOAD 1 " + s[3 + incr])
|
||||||
l.append("OR R2 R0 R1")
|
l.append("OR 2 0 1")
|
||||||
case "NOT":
|
case "NOT":
|
||||||
l.append("LOAD R0 "+s[2+incr])
|
l.append(optionalFlag + "LOAD 0 " + s[2 + incr])
|
||||||
l.append("NOT R2 R0")
|
l.append("NOT 2 0")
|
||||||
""" R2 contiendra la valeur qui dit s'il faut sauter ou non """
|
""" R2 will contain the information whether to jump or not"""
|
||||||
|
|
||||||
return l
|
return l
|
||||||
|
|
||||||
|
@ -107,6 +116,7 @@ fileInput.close()
|
||||||
ASMLinesLabel = ASMLines[:] # will contain at the end of the first loop the code with labels inserted
|
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
|
ASMLinesRegister = [] # will contain at the end of the 2nd loop the registry-based code with labels
|
||||||
ASMLinesFinal = [] # will contain the output, register-based, code
|
ASMLinesFinal = [] # will contain the output, register-based, code
|
||||||
|
|
||||||
for i, l in enumerate(ASMLines):
|
for i, l in enumerate(ASMLines):
|
||||||
items = l.split(" ")
|
items = l.split(" ")
|
||||||
if items[0] in ["JMP", "JMF"]:
|
if items[0] in ["JMP", "JMF"]:
|
||||||
|
@ -119,11 +129,11 @@ for i, l in enumerate(ASMLines):
|
||||||
ASMLinesLabel[lineToJumpTo] = f"{labelCount}_LABEL " + ASMLines[lineToJumpTo]
|
ASMLinesLabel[lineToJumpTo] = f"{labelCount}_LABEL " + ASMLines[lineToJumpTo]
|
||||||
ASMLinesLabel[i] = " ".join(ASMLinesLabel[i].split()[:-1] + [f"{labelCount}_LABEL"])
|
ASMLinesLabel[i] = " ".join(ASMLinesLabel[i].split()[:-1] + [f"{labelCount}_LABEL"])
|
||||||
labelCount += 1
|
labelCount += 1
|
||||||
print(ASMLinesLabel)
|
print("labels : ", ASMLinesLabel)
|
||||||
|
|
||||||
for i, l in enumerate(ASMLinesLabel):
|
for i, l in enumerate(ASMLinesLabel):
|
||||||
ASMLinesRegister.extend(convertToRegister(l.split()))
|
ASMLinesRegister.extend(convertToRegister(l.split()))
|
||||||
print(ASMLinesRegister)
|
print("regs : ", ASMLinesRegister)
|
||||||
|
|
||||||
labels = {}
|
labels = {}
|
||||||
for i, l in enumerate(ASMLinesRegister):
|
for i, l in enumerate(ASMLinesRegister):
|
||||||
|
@ -136,12 +146,18 @@ for i, l in enumerate(ASMLinesRegister):
|
||||||
label = re.match(r"\d_LABEL", l.split()[-1])
|
label = re.match(r"\d_LABEL", l.split()[-1])
|
||||||
if label:
|
if label:
|
||||||
ASMLinesFinal.append(" ".join(l.split()[:-1] + [str(labels[label[0]])]))
|
ASMLinesFinal.append(" ".join(l.split()[:-1] + [str(labels[label[0]])]))
|
||||||
else :
|
else:
|
||||||
ASMLinesFinal.append(l)
|
ASMLinesFinal.append(l)
|
||||||
|
|
||||||
|
print(ASMLinesFinal)
|
||||||
|
output(ASMLinesFinal, 2)
|
||||||
|
|
||||||
print(ASMLinesRegister)
|
lines = []
|
||||||
output(ASMLinesFinal)
|
for i, l in enumerate(ASMLinesFinal):
|
||||||
# - trucs en registre
|
arr = l.split()
|
||||||
# - décaler les Jumps
|
while len(arr) < 4:
|
||||||
# - COP -> OPCode
|
arr.append(0)
|
||||||
|
lines.append(f"(x\"{opToBinOP[arr[0]]}{int(arr[1]):02X}{int(arr[2]):02X}{int(arr[3]):02X}\")")
|
||||||
|
ASMLinesConverted = "(" + ",".join(lines) + ",others => (x\"ff000000\")"
|
||||||
|
print("converted to VHDL-friendly format : " + ASMLinesConverted)
|
||||||
|
output(ASMLinesConverted, 3, True)
|
||||||
|
|
Loading…
Reference in a new issue