public override void Codegen(CIntermediateLang cil, IndentingStringBuilder sb) { cil.DeclareLocalVariable(this); if (AssigningValue == null) { sb.AppendLine(GetDeclaration() + ";"); } else { sb.LineDecl(SourceInfo); AssigningValue.Codegen(cil, sb); var tmp = cil.LastUsedVar; sb.AppendLine(string.Format("{0} = {1};", GetDeclaration(), tmp)); } }
public override void Codegen(CIntermediateLang cil, IndentingStringBuilder sb) { var tmp = cil.CurrentFunction; cil.CurrentFunction = this; sb.LineDecl(SourceInfo); sb.AppendLine(GetDeclaration()); sb.AppendLine("{"); sb.Indent(); cil.PushScope(); foreach (var par in Params) { cil.DeclareLocalVariable(par); } foreach (var stmt in Body) { stmt.Codegen(cil, sb); } cil.PopScope(); sb.Dedent(); sb.AppendLine("}"); cil.CurrentFunction = tmp; }
private CILExpression CreateCtorCall(CIntermediateLang cil) { var call = Expression as AstCall; var ctorId = call?.Callee as AstIdent; if (ctorId == null) { throw new NotImplementedException(); } var @sizeof = new AstCall(SourceInfo, new AstIdent(SourceInfo, "sizeof"), new List <AstExpression> { new AstIdent(SourceInfo, ctorId.Name) }); var malloc = new AstCall(SourceInfo, new AstIdent(SourceInfo, "alloc"), new List <AstExpression> { @sizeof }); var tmp = NameGenerator.NewTemp(); var tmpType = new AstType(SourceInfo, ctorId.Name, 1, 0, false, false); var decl = new AstDeclaration(SourceInfo, tmp, tmpType, malloc); var rewrite = new List <CILNode>(); var cdecl = decl.ToCILVariableDeclAndDecl(cil); cil.DeclareLocalVariable(cdecl); rewrite.Add(cdecl); var ctor = call.FixIdent(cil, string.Format("{0}_{1}", ctorId.Name, "Ctor")); call.Args.Insert(0, new AstIdent(SourceInfo, decl.Name)); ctorId.Name = ctor; var ctorCall = call.ToCILExpression(cil); rewrite.Add(ctorCall); var objId = new AstIdent(SourceInfo, decl.Name); rewrite.Add(objId.ToCILExpression(cil)); return(new CILRewriteExpression(SourceInfo, rewrite)); }