public override Value CodeGen(IRBuilder builder) { Value l = this.LHS.CodeGen(builder); Value r = this.RHS.CodeGen(builder); if(l.IsNull || r.IsNull) return Value.Null; switch(this.Op) { case '+': return builder.BuildFAdd(l, r); case '-': return builder.BuildFSub(l, r); case '*': return builder.BuildFMul(l, r); case '<': // Convert bool 0/1 to double 0.0 or 1.0 return builder.BuildFCmpAndPromote(l, LLVMRealPredicate.RealULT, r, TypeRef.CreateDouble()); } // If it wasn't a builtin binary operator, it must be a user defined one. Emit // a call to it. Function f = CodeGenManager.Module.GetFunction("binary" + this.Op); Debug.Assert(f != null); Value[] ops = new Value[] { l, r }; return builder.BuildCall(f, ops, "binop"); }
public override Value CodeGen(IRBuilder builder) { Value l = this.LHS.CodeGen(builder); Value r = this.RHS.CodeGen(builder); if(l.IsNull || r.IsNull) return Value.Null; switch(this.Op) { case '+': return builder.BuildFAdd(l, r); case '-': return builder.BuildFSub(l, r); case '*': return builder.BuildFMul(l, r); case '<': // Convert bool 0/1 to double 0.0 or 1.0 return builder.BuildFCmpAndPromote(l, LLVMRealPredicate.RealULT, r, TypeRef.CreateDouble()); } CodeGenManager.ErrorOutput.WriteLine("Unknown binary operator."); return Value.Null; }