mirror of
https://github.com/hazemKrimi/jack-vm-translator.git
synced 2026-05-01 18:00:27 +00:00
Fixing call wip
This commit is contained in:
+17
-15
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
int callCounter = 0;
|
||||||
|
|
||||||
string translateFunction(string name, int args)
|
string translateFunction(string name, int args)
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
@@ -27,7 +29,9 @@ string translateFunction(string name, int args)
|
|||||||
string translateCall(string name, int args)
|
string translateCall(string name, int args)
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
string label = name + "$ret" + generateRandomLabel(3);
|
string label = name + "$ret" + std::to_string(callCounter);
|
||||||
|
|
||||||
|
callCounter++;
|
||||||
|
|
||||||
output << "@" << label << endl;
|
output << "@" << label << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
@@ -71,15 +75,15 @@ string translateCall(string name, int args)
|
|||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@R" << endl;
|
output << "@C" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
output << "@5" << endl;
|
output << "@5" << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@R" << endl;
|
output << "@C" << endl;
|
||||||
output << "M=M-D" << endl;
|
output << "M=M-D" << endl;
|
||||||
output << "@" << args << endl;
|
output << "@" << args << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@R" << endl;
|
output << "@C" << endl;
|
||||||
output << "M=M-D" << endl;
|
output << "M=M-D" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@ARG" << endl;
|
output << "@ARG" << endl;
|
||||||
@@ -100,23 +104,21 @@ string translateCall(string name, int args)
|
|||||||
string translateReturn()
|
string translateReturn()
|
||||||
{
|
{
|
||||||
stringstream output;
|
stringstream output;
|
||||||
string cleanupLabel = "cleanup$ret" + generateRandomLabel(3);
|
|
||||||
string endLabel = "end$ret" + generateRandomLabel(3);
|
|
||||||
|
|
||||||
output << "@LCL" << endl;
|
output << "@LCL" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@END_FRAME" << endl;
|
output << "@EF" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@RETURN_ADDR" << endl;
|
output << "@RA" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
output << "@5" << endl;
|
output << "@5" << endl;
|
||||||
output << "D=A" << endl;
|
output << "D=A" << endl;
|
||||||
output << "@RETURN_ADDR" << endl;
|
output << "@RA" << endl;
|
||||||
output << "M=M-D" << endl;
|
output << "M=M-D" << endl;
|
||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@RETURN_ADDR" << endl;
|
output << "@RA" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@ARG" << endl;
|
output << "@ARG" << endl;
|
||||||
@@ -136,7 +138,7 @@ string translateReturn()
|
|||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@END_FRAME" << endl;
|
output << "@EF" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@1" << endl;
|
output << "@1" << endl;
|
||||||
output << "D=D-A" << endl;
|
output << "D=D-A" << endl;
|
||||||
@@ -145,7 +147,7 @@ string translateReturn()
|
|||||||
output << "@THAT" << endl;
|
output << "@THAT" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@END_FRAME" << endl;
|
output << "@EF" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@2" << endl;
|
output << "@2" << endl;
|
||||||
output << "D=D-A" << endl;
|
output << "D=D-A" << endl;
|
||||||
@@ -154,7 +156,7 @@ string translateReturn()
|
|||||||
output << "@THIS" << endl;
|
output << "@THIS" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@END_FRAME" << endl;
|
output << "@EF" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@3" << endl;
|
output << "@3" << endl;
|
||||||
output << "D=D-A" << endl;
|
output << "D=D-A" << endl;
|
||||||
@@ -163,7 +165,7 @@ string translateReturn()
|
|||||||
output << "@ARG" << endl;
|
output << "@ARG" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@END_FRAME" << endl;
|
output << "@EF" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@4" << endl;
|
output << "@4" << endl;
|
||||||
output << "D=D-A" << endl;
|
output << "D=D-A" << endl;
|
||||||
@@ -172,7 +174,7 @@ string translateReturn()
|
|||||||
output << "@LCL" << endl;
|
output << "@LCL" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@RETURN_ADDR" << endl;
|
output << "@RA" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "D;JMP" << endl;
|
output << "D;JMP" << endl;
|
||||||
|
|
||||||
|
|||||||
+15
-15
@@ -14,7 +14,7 @@ string translateAdd()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@A" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -22,7 +22,7 @@ string translateAdd()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@A" << endl;
|
||||||
output << "M=D+M" << endl;
|
output << "M=D+M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ string translateSub()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@S" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -52,7 +52,7 @@ string translateSub()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@S" << endl;
|
||||||
output << "M=D-M" << endl;
|
output << "M=D-M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
@@ -74,7 +74,7 @@ string translateNeg()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@N" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
output << "M=M-D" << endl;
|
output << "M=M-D" << endl;
|
||||||
output << "M=M-D" << endl;
|
output << "M=M-D" << endl;
|
||||||
@@ -102,7 +102,7 @@ string translateEq()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@E" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -110,7 +110,7 @@ string translateEq()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@E" << endl;
|
||||||
output << "M=D-M" << endl;
|
output << "M=D-M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@" << LABEL << "_TRUE" << endl;
|
output << "@" << LABEL << "_TRUE" << endl;
|
||||||
@@ -154,7 +154,7 @@ string translateGt()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@GT" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -162,7 +162,7 @@ string translateGt()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@GT" << endl;
|
||||||
output << "M=D-M" << endl;
|
output << "M=D-M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@" << LABEL << "_TRUE" << endl;
|
output << "@" << LABEL << "_TRUE" << endl;
|
||||||
@@ -208,7 +208,7 @@ string translateLt()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@LT" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -216,7 +216,7 @@ string translateLt()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@LT" << endl;
|
||||||
output << "M=D-M" << endl;
|
output << "M=D-M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
output << "@" << LABEL << "_TRUE" << endl;
|
output << "@" << LABEL << "_TRUE" << endl;
|
||||||
@@ -258,7 +258,7 @@ string translateAnd()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@AND" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -266,7 +266,7 @@ string translateAnd()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@AND" << endl;
|
||||||
output << "M=D&M" << endl;
|
output << "M=D&M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
@@ -288,7 +288,7 @@ string translateOr()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@OR" << endl;
|
||||||
output << "M=D" << endl;
|
output << "M=D" << endl;
|
||||||
|
|
||||||
output << "@SP" << endl;
|
output << "@SP" << endl;
|
||||||
@@ -296,7 +296,7 @@ string translateOr()
|
|||||||
output << "A=M" << endl;
|
output << "A=M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
output << "@R" << endl;
|
output << "@OR" << endl;
|
||||||
output << "M=D|M" << endl;
|
output << "M=D|M" << endl;
|
||||||
output << "D=M" << endl;
|
output << "D=M" << endl;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user