private CILExpression CreateDtorCall(CIntermediateLang cil) { var rewrite = new List <CILNode>(); var dtorTy = Expression.TryInferType(cil); var dtor = string.Format("{0}_{1}_{0}p", dtorTy.Name, "Dtor"); var dtorSym = LllCompiler.SymTable.LookupSymbol(dtor); var dtorExt = dtorSym?.Extra as AstExtend; if (dtorExt != null && !dtorExt.CanOverride) { var callDtor = new AstCall(SourceInfo, new AstIdent(SourceInfo, dtor), new List <AstExpression> { Expression }); rewrite.Add(callDtor.ToCILExpression(cil)); } var callFree = new AstCall(SourceInfo, new AstIdent(SourceInfo, "free"), new List <AstExpression> { Expression }); rewrite.Add(callFree.ToCILExpression(cil)); return(new CILRewriteExpression(SourceInfo, rewrite)); }
public override LllType TryInferType(CIntermediateLang cil) { var lhsTy = Lhs.TryInferType(cil); var rhsTy = Rhs.TryInferType(cil); if (lhsTy is LllIntegerType && rhsTy is LllIntegerType) { if (Rhs is AstNumber) { return(lhsTy); } if (Lhs is AstNumber) { return(rhsTy); } } if (lhsTy.Equals(rhsTy)) { return(lhsTy); } if (rhsTy.TryCast(lhsTy)) { return(lhsTy); } if (lhsTy.TryCast(rhsTy)) { return(rhsTy); } // not primitive, make a call to lhs.__op__(rhs); var opAlias = _binOpInfo[Op].CallAlias; var member = new AstMemberAccess(SourceInfo, Lhs, opAlias); var opCall = new AstCall(SourceInfo, member, new List <AstExpression> { Rhs }); return(opCall.TryInferType(cil)); }
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)); }
public override CILExpression ToCILExpression(CIntermediateLang cil) { if (IsConditionalOp()) { return(ToCILCondition(cil)); } var lhsTy = Lhs.TryInferType(cil); var lhs = lhsTy.IsAReference ? new CILDereference(Lhs.SourceInfo, Lhs.ToCILExpression(cil)) : Lhs.ToCILExpression(cil); var rhsTy = Rhs.TryInferType(cil); var rhs = rhsTy.IsAReference ? new CILDereference(Rhs.SourceInfo, Rhs.ToCILExpression(cil)) : Rhs.ToCILExpression(cil); // primitives, just use the builtin operators if (lhsTy.IsPrimitive && rhsTy.IsPrimitive) { return(new CILBinaryOp( SourceInfo, lhs, _binOpInfo[Op].CILOpType, rhs)); } // not primitive, make a call to lhs.__op__(rhs); var opAlias = _binOpInfo[Op].CallAlias; var member = new AstMemberAccess(SourceInfo, Lhs, opAlias); var opCall = new AstCall(SourceInfo, member, new List <AstExpression> { Rhs }); return(opCall.ToCILExpression(cil)); }