+/TECS
git clone https://git.julia.blue/+/TECS
c8053ff
master
1# Translates vm code and writes assembly to output file 2class CodeWriter : 3 4def __init__ (self ,fileName ): 5self .f = open (fileName + ".asm" ,'w' ) 6self .labelNum = {"eq" :0 ,"lt" :0 ,"gt" :0 ,"call" :0 } 7self .currentFunction = "" 8 9def setFile (self ,fileName ): 10self .currentFile = fileName .split ('/' )[- 1 ].split ('.' )[0 ] 11 12def writeInit (self ): 13self .f .write ("@256\nD=A\n@SP\nM=D\n" ) 14self .writeCall ("Sys.init" ,0 ) 15self .f .write ("@HALTLOOP\n0;JMP\n" ) 16 17def writeLabel (self ,lbl ): 18self .f .write ('(' + self .currentFunction + '$' + lbl + ')\n' ) 19 20def writeGoto (self ,lbl ): 21self .f .write ('@' + self .currentFunction + '$' + lbl + "\n0;JMP\n" ) 22 23def writeIf (self ,lbl ): 24self .f .write ("@SP\nAM=M-1\nD=M\n@" + 25self .currentFunction + '$' + lbl + "\nD;JNE\n" ) 26 27def writeFunction (self ,functionName ,localNum ): 28self .currentFunction = functionName 29self .f .write ("(" + self .currentFunction + ")\n" ) 30if localNum == 1 : 31self .f .write ("@0\nD=A\nM=M+1\nA=M-1\nM=D\n" ) 32elif localNum > 1 : 33self .f .write ("@" + str (localNum )+ "\nD=A\n@SP\nM=D+M\nD=A\nA=M-1\nM=D\n" + 34"A=A-1\nM=D\n" * (localNum - 1 )) 35 36def writeReturn (self ): 37self .f .write ("@RETURNLBL\n0;JMP\n" ) 38 39def writeCall (self ,functionName ,argNum ): 40returnAddress = "ReturnAddress" + str (self .labelNum ["call" ]) 41self .f .write ("@" + functionName + "\nD=A\n@R13\nM=D\n@" + str (argNum + 5 )+ 42"\nD=A\n@R14\nM=D\n@" + returnAddress + "\nD=A\n@CALLLBL\n0;JMP\n(" + returnAddress + ")\n" ) 43self .labelNum ["call" ]+= 1 44 45def writeArithmetic (self ,command ): 46if command in {"neg" ,"not" }: 47self .f .write ("@SP\nA=M-1\nM=" ) 48if command == "neg" : 49self .f .write ("-M\n" ) 50else : 51self .f .write ("!M\n" ) 52else : 53self .f .write ("@SP\nAM=M-1\nD=M\nA=A-1\n" ) 54if command == "or" : 55self .f .write ("M=D|M\n" ) 56elif command == "and" : 57self .f .write ("M=D&M\n" ) 58elif command == "add" : 59self .f .write ("M=D+M\n" ) 60elif command == "sub" : 61self .f .write ("M=M-D\n" ) 62elif command in {"gt" ,"lt" ,"eq" }: 63self .f .write ("D=M-D\n@R13\nM=D\n@" + command .upper ()+ str (self .labelNum [command ])+ "\nD=A\n@R14\nM=D\n@" + command .upper ( 64 )+ "LBL\n0;JMP\n(" + command .upper ()+ str (self .labelNum [command ])+ ")\n" ) 65self .labelNum [command ]+= 1 66 67def writePushPop (self ,command ,segment ,index ): 68segmentNames = {"local" :"LCL" ,"argument" :"ARG" , 69"this" :"THIS" ,"that" :"THAT" ,"temp" :"R5" ,"pointer" :"R3" } 70if command == "C_PUSH" : 71if segment == "constant" : 72self .f .write ("@" + str (index )+ "\nD=A\n" ) 73elif segment == "static" : 74self .f .write ("@" + self .currentFile + str (index )+ "\nD=M\n" ) 75else : 76self .f .write ("@" + str (segmentNames [segment ])+ "\n" ) 77if segment not in {"pointer" ,"temp" }: 78self .f .write ("A=M\n" ) 79if index > 0 : 80self .f .write ("D=A\n@" + str (index )+ "\nD=D+A\nA=D\n" ) 81self .f .write ("D=M\n" ) 82self .f .write ("@SP\nM=M+1\nA=M-1\nM=D\n" ) 83else : 84if segment == "static" : 85self .f .write ("@SP\nAM=M-1\nD=M\n@" + 86self .currentFile + str (index )+ "\nM=D\n" ) 87elif index < 6 : 88self .f .write ("@SP\nAM=M-1\nD=M\n@" + 89str (segmentNames [segment ])+ "\n" ) 90if segment not in {"pointer" ,"temp" }: 91self .f .write ("A=M\n" ) 92for i in range (index ): 93self .f .write ("A=A+1\n" ) 94self .f .write ("M=D\n" ) 95else : 96self .f .write ("@" + str (index )+ "\nD=A\n@" + 97str (segmentNames [segment ])+ "\nD=D+" ) 98self .f .write ("M" if segment not in { 99"pointer" ,"temp" }else "A" ) 100self .f .write ("\n@R13\nM=D\n@SP\nAM=M-1\nD=M\n@R13\nA=M\nM=D\n" ) 101 102def close (self ): 103self .f .write ("(HALTLOOP)\n@HALTLOOP\n0;JMP\n(EQLBL)\n@R13\nD=M\n@TRUELBL\nD;JEQ\n@FALSELBL\n0;JMP\n(GTLBL)\n@R13\nD=M\n@TRUELBL\nD;JGT\n@FALSELBL\n0;JMP\n(LTLBL)\n@R13\nD=M\n@TRUELBL\nD;JLT\n@FALSELBL\n0;JMP\n(TRUELBL)\n@SP\nA=M-1\nM=-1\n@14\nA=M\n0;JMP\n(FALSELBL)\n@SP\nA=M-1\nM=0\n@14\nA=M\n0;JMP\n(RETURNLBL)\n@5\nD=A\n@LCL\nA=M-D\nD=M\n@R13\nM=D\n@SP\nA=M-1\nD=M\n@ARG\nA=M\nM=D\nD=A+1\n@SP\nM=D\n@LCL\nAM=M-1\nD=M\n@THAT\nM=D\n@LCL\nAM=M-1\nD=M\n@THIS\nM=D\n@LCL\nAM=M-1\nD=M\n@ARG\nM=D\n@LCL\nA=M-1\nD=M\n@LCL\nM=D\n@R13\nA=M\n0;JMP\n(CALLLBL)\n@SP\nM=M+1\nA=M-1\nM=D\n@LCL\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n@ARG\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n@THIS\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n@THAT\nD=M\n@SP\nM=M+1\nA=M-1\nM=D\n@R14\nD=M\n@SP\nD=M-D\n@ARG\nM=D\n@SP\nD=M\n@LCL\nM=D\n@R13\nA=M\n0;JMP\n" ) 104self .f .close ()