private void ILSimpleBinary(QuadTuple qt, string op) { var tplt1 = " mov eax,{0}"; if (qt.RValueA.ValueType == "addr") { ProcSegment.Add(string.Format(tplt1, "dword ptr " + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); } var tplt2 = " {0} eax,{1}"; if (qt.RValueB.ValueType == "addr") { ProcSegment.Add(string.Format(tplt2, op, "dword ptr " + qt.RValueB.ID)); } else { ProcSegment.Add(string.Format(tplt2, op, qt.RValueB.ID)); } var tplt3 = " mov {0},eax"; var target = qt.LValue.ID; if (!LSymbols.ContainsKey(target) && !GSymbols.ContainsKey(target)) { ProcSegment.Insert(0, string.Format(" local {0}:dword", target)); LSymbols[target] = "int"; } ProcSegment.Add(string.Format(tplt3, target)); }
private void ILAssign(QuadTuple qt) { var tplt1 = " mov eax,{0}"; if (qt.RValueA.ValueType == "addr") { ProcSegment.Add(string.Format(tplt1, "dword ptr " + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); } var target = qt.LValue.ID; if (!LSymbols.ContainsKey(target) && !GSymbols.ContainsKey(target)) { ProcSegment.Insert(0, string.Format(" local {0}:dword", target)); LSymbols[target] = "int"; } if (qt.LValue.ValueType == "addr") { var tplt3 = " mov ebx,{0}"; ProcSegment.Add(string.Format(tplt3, qt.LValue.ID)); ProcSegment.Add(" mov [ebx],eax"); } else { var tplt2 = " mov {0},eax"; ProcSegment.Add(string.Format(tplt2, target)); } }
private void ILCall(QuadTuple qt) { var tplt = " invoke {0}"; if (qt.RValueA.ID == "scanf" || qt.RValueA.ID == "printf") { ProcSegment.Add(string.Format(tplt, "crt_" + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt, qt.RValueA.ID)); } if (PushQueue is not null) { foreach (var i in PushQueue) { ProcSegment[ProcSegment.Count - 1] += i; } PushQueue = null; } if (qt.LValue is not null) { var ret = qt.LValue.ID; if (!LSymbols.ContainsKey(ret) && !GSymbols.ContainsKey(ret)) { ProcSegment.Insert(0, string.Format(" local {0}:dword", ret)); LSymbols[ret] = "int"; } var tplt4 = " mov {0},eax"; ProcSegment.Add(string.Format(tplt4, ret)); } }
private void ILJc(QuadTuple qt, string op) { var tplt1 = " mov eax,{0}"; if (qt.RValueA.ValueType == "addr") { ProcSegment.Add(string.Format(tplt1, "dword ptr " + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); } var tplt3 = " mov ebx,{0}"; if (qt.RValueB.ValueType == "addr") { ProcSegment.Add(string.Format(tplt3, "dword ptr " + qt.RValueB.ID)); } else { ProcSegment.Add(string.Format(tplt3, qt.RValueB.ID)); } ProcSegment.Add(" cmp eax,ebx"); var tplt2 = " {0} {1}"; ProcSegment.Add(string.Format(tplt2, op, qt.LValue.ID)); }
private void ILDivMod(QuadTuple qt, string op) { ProcSegment.Add(" xor edx,edx"); var tplt1 = " mov eax,{0}"; if (qt.RValueA.ValueType == "addr") { ProcSegment.Add(string.Format(tplt1, "dword ptr " + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); } var tplt2 = " mov ebx,{0}"; if (qt.RValueB.ValueType == "addr") { ProcSegment.Add(string.Format(tplt2, "dword ptr " + qt.RValueB.ID)); } else { ProcSegment.Add(string.Format(tplt2, qt.RValueB.ID)); } ProcSegment.Add(" idiv ebx"); var tplt3 = " mov {0},{1}"; var target = qt.LValue.ID; if (!LSymbols.ContainsKey(target) && !GSymbols.ContainsKey(target)) { ProcSegment.Insert(0, string.Format(" local {0}:dword", target)); LSymbols[target] = "int"; } ProcSegment.Add(string.Format(tplt3, target, op)); }
//calc & store the addr of the element private void ILArrayAccess(QuadTuple qt) { var tplt1 = " mov eax,{0}"; ProcSegment.Add(string.Format(tplt1, qt.RValueB.ID)); if (qt.LValue.ValueType == "int" || qt.LValue.ValueType == "addr") { ProcSegment.Add(string.Format(" imul eax,eax,{0}", 4)); } var arrName = qt.RValueA.ID; ProcSegment.Add(string.Format(" lea ebx,{0}", arrName)); ProcSegment.Add(" mov eax,[eax][ebx]"); var tplt2 = " mov {0},eax"; var target = qt.LValue.ID; if (!LSymbols.ContainsKey(target) && !GSymbols.ContainsKey(target)) { if (qt.LValue.ValueType == "int" || qt.LValue.ValueType == "addr") { LSymbols[target] = "int"; ProcSegment.Insert(0, string.Format(" local {0}:dword", target)); } } ProcSegment.Add(string.Format(tplt2, target)); }
private void ILJne(QuadTuple qt) { var tplt1 = " cmp {0},{1}"; ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID, qt.RValueB.ID)); var tplt2 = " jne {0}"; ProcSegment.Add(string.Format(tplt2, qt.LValue.ID)); }
private void ILReturn(QuadTuple qt) { if (qt.LValue is not null) { var tplt1 = " mov eax,{0}"; ProcSegment.Add(string.Format(tplt1, qt.LValue.ID)); } var tplt2 = " ret"; ProcSegment.Add(tplt2); }
private void ILArrayDefine(QuadTuple qt) { var tplt = "{0} {1} {2} DUP({3})"; var vtype = qt.LValue.ValueType; var len = int.Parse(qt.RValueA.ID); if (vtype == "int") { GSymbols[qt.LValue.ID] = qt.LValue.ValueType; DataSegment.Insert(1, string.Format(tplt, qt.LValue.ID, "dword", len, 0)); } }
private void ILDecrease(QuadTuple qt) { var tplt = " dec {0}"; if (qt.LValue.ValueType == "addr") { ProcSegment.Add(string.Format(tplt, "dword ptr " + qt.LValue.ID)); } else { ProcSegment.Add(string.Format(tplt, qt.LValue.ID)); } }
private void ILParam(QuadTuple qt) { var last = CodeSegment[CodeSegment.Count - 1]; if (last[last.Length - 1] == ' ') { CodeSegment[CodeSegment.Count - 1] += string.Format("{0}:dword", qt.LValue.ID); } else { CodeSegment[CodeSegment.Count - 1] += string.Format(",{0}:dword", qt.LValue.ID); } }
private void ILArrayAssign(QuadTuple qt) { var tplt1 = " lea eax,{0}"; var offset = qt.RValueA.ID; ProcSegment.Add(string.Format(tplt1, offset)); var tplt2 = " mov ebx,{0}"; offset = qt.RValueB.ID; ProcSegment.Add(string.Format(tplt2, offset)); ProcSegment.Add(" imul ebx,ebx,4"); ProcSegment.Add(" add eax,ebx"); var tplt3 = " mov {0},eax"; ProcSegment.Add(string.Format(tplt3, qt.LValue.ID)); }
private void ILPush(QuadTuple qt) { var tplt = ",{0}"; if (PushQueue is null) { PushQueue = new List <string>(); } if (qt.LValue.ValueType == "string") { PushQueue.Add(string.Format(tplt, "offset " + qt.LValue.ID)); } else { PushQueue.Add(string.Format(tplt, qt.LValue.ID)); } }
public static void PrintToConsole(this QuadTuple i) { Console.Write("({0},", i.Operator); if (i.RValueA is not null) { Console.Write(i.RValueA.ID); } Console.Write(","); if (i.RValueB is not null) { Console.Write(i.RValueB.ID); } Console.Write(","); if (i.LValue is not null) { Console.Write(i.LValue.ID); } Console.Write(")\n"); }
private void ILLoadAddress(QuadTuple qt) { var tplt1 = " lea eax,{0}"; ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); if (qt.RValueB is not null) { var tplt3 = " mov ebx,{0}"; ProcSegment.Add(string.Format(tplt3, qt.RValueB.ID)); ProcSegment.Add(" imul ebx,ebx,4"); ProcSegment.Add(" add eax,ebx"); } var tplt2 = " mov {0},eax"; var target = qt.LValue.ID; if (!LSymbols.ContainsKey(target) && !GSymbols.ContainsKey(target)) { LSymbols[target] = "addr"; ProcSegment.Insert(0, string.Format(" local {0}:dword", target)); } ProcSegment.Add(string.Format(tplt2, target)); }
private void ILDataEnd(QuadTuple qt) { DataSegment.Add(".code"); }
public static void InjectConstant(this QuadTuple qt, ASTConstant constant, int isRValue, ILIdentifier str = null) { if (constant is ASTStringConstant sc) { if (isRValue == 0) { qt.RValueA = str; } else if (isRValue == 1) { qt.RValueB = str; } else { qt.LValue = str; } } else if (constant is ASTIntegerConstant ic) { if (isRValue == 0) { qt.RValueA = new ILIdentifier(ic.Value.ToString(), ILNameType.Constant, "int"); } else if (isRValue == 1) { qt.RValueB = new ILIdentifier(ic.Value.ToString(), ILNameType.Constant, "int"); } else { qt.LValue = new ILIdentifier(ic.Value.ToString(), ILNameType.Constant, "int"); } } else if (constant is ASTFloatConstant fp) { if (isRValue == 0) { qt.RValueA = new ILIdentifier(fp.Value.ToString(), ILNameType.Constant, "float"); } else if (isRValue == 1) { qt.RValueB = new ILIdentifier(fp.Value.ToString(), ILNameType.Constant, "float"); } else { qt.LValue = new ILIdentifier(fp.Value.ToString(), ILNameType.Constant, "float"); } } else if (constant is ASTCharConstant cc) { if (isRValue == 0) { qt.RValueA = new ILIdentifier(cc.Value.ToString(), ILNameType.Constant, "char"); } else if (isRValue == 1) { qt.RValueB = new ILIdentifier(cc.Value.ToString(), ILNameType.Constant, "char"); } else { qt.LValue = new ILIdentifier(cc.Value.ToString(), ILNameType.Constant, "char"); } } }
private void ILVarDefine(QuadTuple qt) { var tplt = "{0} {1} {2}"; var vtype = qt.LValue.ValueType; var name = qt.LValue.ID; if (vtype == "int") { if (isLocal) { if (!LSymbols.ContainsKey(name)) { var tplt_local = " local {0}:{1}"; LSymbols[name] = vtype; ProcSegment.Insert(0, string.Format(tplt_local, name, "dword")); } if (qt.RValueA is not null) { if (qt.RValueA.ILNameType == ILNameType.Constant) { var tplt1 = " mov {0},{1}"; ProcSegment.Add(string.Format(tplt1, name, qt.RValueA.ID)); } else { var tplt1 = " mov eax,{0}"; var tplt2 = " mov {0},eax"; if (qt.RValueA.ValueType == "addr") { ProcSegment.Add(string.Format(tplt1, "dword ptr " + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); } ProcSegment.Add(string.Format(tplt2, name)); } } else { ProcSegment.Add(string.Format(" mov {0},0", name)); } } else { GSymbols[name] = vtype; if (qt.RValueA is not null) { if (qt.RValueA.ILNameType == ILNameType.Constant) { var tplt1 = " mov {0},{1}"; ProcSegment.Add(string.Format(tplt1, name, qt.RValueA.ID)); } else { var tplt1 = " mov eax,{0}"; var tplt2 = " mov {0},eax"; if (qt.RValueA.ValueType == "addr") { ProcSegment.Add(string.Format(tplt1, "dword ptr " + qt.RValueA.ID)); } else { ProcSegment.Add(string.Format(tplt1, qt.RValueA.ID)); } ProcSegment.Add(string.Format(tplt2, name)); } } else { DataSegment.Add(string.Format(tplt, name, "dword", 0)); } } } else if (vtype == "string") { GSymbols[name] = "string"; var tplt1 = "{0} byte {1},0"; var str = qt.RValueA.ID; if (str.Length >= 4 && str[str.Length - 3] == '\\' && str[str.Length - 2] == 'n') { str = str.Remove(str.Length - 3, 2); if (str.Length <= 2) { DataSegment.Add(string.Format(tplt1, name, "0ah")); } else { DataSegment.Add(string.Format(tplt1, name, str + ",0ah")); } } else { DataSegment.Add(string.Format(tplt1, name, str)); } } }
private void ILProcEnd(QuadTuple qt) { var tplt = "{0} endp"; CodeSegment.Add(string.Format(tplt, qt.LValue.ID)); }
private void ILJmp(QuadTuple qt) { ProcSegment.Add(string.Format(" jmp {0}", qt.LValue.ID)); }
private void ILJmpTarget(QuadTuple qt) { ProcSegment.Add(string.Format("{0}:", qt.LValue.ID)); }
private void ILDataBegin(QuadTuple qt) { DataSegment.Add(".data"); }
private void ILProcBegin(QuadTuple qt) { var tplt = "{0} proc stdcall "; CodeSegment.Add(string.Format(tplt, qt.LValue.ID)); }