public string Visit(CIL_AST_Root node) { Utils.SetMemory(mem, semtype, node.Types); allnode = new List <CIL_AST_Node>(CIL_AST_Node.GetAllNode(node)); int main = -1; for (int i = 0; i < allnode.Count; i++) { if (allnode[i] is CIL_Label) { resolvaddr[((CIL_Label)allnode[i]).Label] = i; } else if (allnode[i] is CIL_FunctionDef) { resolvaddr[((CIL_FunctionDef)allnode[i]).Name] = i; if (main == -1) { main = i; } //mem.SetValue(mem.GetDirMethod(((CIL_ClassMethod)allnode[i]).Idres), i); } } foreach (var item in semtype) { if (item.Key == "SELF_TYPE") { continue; } int idx = mem.GetDirType(item.Key); idx += 2; foreach (var item2 in item.Value.GetAllMethods()) { if (resolvaddr.ContainsKey(item2.Label())) { mem.SetValue(idx++, resolvaddr[item2.Label()]); } else { Console.WriteLine($"Method {item2.Label()} not found in CIL"); idx++; } } //Console.Write($"{item.Key} :"); //PrintObject(mem.GetDirType(item.Key)); } // Build DataElement node.Data.Visit(this); // Debug DataElement foreach (var d in dataaddr) { //PrintObject(d.Value); } // Build OffSetAttr node.Types.Visit(this); // Init Simulator variableaddr.Push(new Dictionary <string, int>()); allnode[resolvaddr["entry"]].Visit(this); return(io.get_all_text()); }
public static void SetMemory(ICIL_Memory mem, Dictionary <string, SemanticType> types, CIL_SectionTypes secttype) { Queue <string> Zeros = new Queue <string>(); var graph = BuildGraph(types); Dictionary <string, int> ind = new Dictionary <string, int>(); foreach (var item in graph) { ind.Add(item.Key, 0); } foreach (var item in graph) { foreach (var w in item.Value) { ind[w]++; } } foreach (var item in ind) { if (item.Value == 0) { Zeros.Enqueue(item.Key); } } // Section Type Dictionary <string, CIL_ClassDef> mapped = new Dictionary <string, CIL_ClassDef>(); foreach (var item in secttype.ListNode) { mapped.Add(item.Id, item); } while (Zeros.Count > 0) { string k = Zeros.Dequeue(); foreach (var item in graph[k]) { ind[item]--; if (ind[item] == 0) { Zeros.Enqueue(item); } } if (!mapped.ContainsKey(k)) { continue; } var classdef = mapped[k]; int idx = mem.Allocate(classdef.Methods.ListNode.Count + 2); mem.SetDirType(k, idx); mem.SetValue(idx++, classdef.Methods.ListNode.Count + 1); var sem = types[k]; if (sem.Father != null) { mem.SetValue(idx, mem.GetDirType(sem.Father.Name)); } idx++; foreach (var item in classdef.Methods.ListNode) { mem.SetDirmethod(item.Idres, idx++); } } }