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 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 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 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 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 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)); } } }