5 // Chris Bacon (chrisbacon76@gmail.com)
7 // Copyright (C) 2010 Chris Bacon
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
34 using Mono.CodeContracts.Rewrite.Ast;
36 namespace Mono.CodeContracts.Rewrite.AstVisitors {
37 class CompileVisitor : ExprVisitor {
39 public CompileVisitor (ILProcessor il, Dictionary<Expr, Instruction> instructionLookup)
40 : this (il, instructionLookup, il.Append)
44 public CompileVisitor (ILProcessor il, Dictionary<Expr, Instruction> instructionLookup, Action<Instruction> fnEmit)
47 this.instructionLookup = instructionLookup;
51 private ILProcessor il;
52 private Dictionary<Expr, Instruction> instructionLookup;
53 private Action<Instruction> fnEmit;
55 private void Emit (Expr originalExpr, Instruction inst)
57 Instruction originalInst;
58 if (this.instructionLookup != null) {
59 // TODO: Doesn't handle inherited contracts - need to check what to do in this case.
60 //if (this.instructionLookup.TryGetValue (originalExpr, out originalInst)) {
61 // inst.SequencePoint = originalInst.SequencePoint;
67 private void Emit (Expr originalExpr, Func<Instruction> fnCreateInstruction)
69 Instruction inst = fnCreateInstruction();
70 this.Emit (originalExpr, inst);
73 private void Emit (Expr originalExpr, Func<IEnumerable<Instruction>> fnCreateInstruction)
75 throw new NotImplementedException ();
78 protected override Expr VisitNop (ExprNop e)
80 var instNop = this.il.Create (OpCodes.Nop);
81 this.Emit (e, instNop);
85 protected override Expr VisitLoadArg (ExprLoadArg e)
91 return this.il.Create (OpCodes.Ldarg_0);
93 return this.il.Create (OpCodes.Ldarg_1);
95 return this.il.Create (OpCodes.Ldarg_2);
97 return this.il.Create (OpCodes.Ldarg_3);
100 return this.il.Create (OpCodes.Ldarg_S, (byte) index);
102 return this.il.Create (OpCodes.Ldarg, index);
110 protected override Expr VisitLoadConstant (ExprLoadConstant e)
112 this.Emit (e, () => {
115 return this.il.Create (OpCodes.Ldnull);
117 Type vType = v.GetType ();
118 TypeCode vTypeCode = Type.GetTypeCode (vType);
124 return this.il.Create (OpCodes.Ldc_I4_M1);
126 return this.il.Create (OpCodes.Ldc_I4_0);
128 return this.il.Create (OpCodes.Ldc_I4_1);
130 return this.il.Create (OpCodes.Ldc_I4_2);
132 return this.il.Create (OpCodes.Ldc_I4_3);
134 return this.il.Create (OpCodes.Ldc_I4_4);
136 return this.il.Create (OpCodes.Ldc_I4_5);
138 return this.il.Create (OpCodes.Ldc_I4_6);
140 return this.il.Create (OpCodes.Ldc_I4_7);
142 return this.il.Create (OpCodes.Ldc_I4_8);
144 if (value >= -128 && value <= 127) {
145 return this.il.Create (OpCodes.Ldc_I4_S, (sbyte) value);
147 return this.il.Create (OpCodes.Ldc_I4, value);
150 case TypeCode.Single:
151 return this.il.Create (OpCodes.Ldc_R4, (float) v);
152 case TypeCode.Double:
153 return this.il.Create (OpCodes.Ldc_R8, (double) v);
154 case TypeCode.String:
155 return this.il.Create (OpCodes.Ldstr, (string) v);
157 throw new NotSupportedException ("Cannot handle constant: " + vTypeCode);
164 private Expr VisitBinary (ExprBinaryOp e, Func<Instruction> fnCreateIl)
167 this.Visit (e.Right);
168 var inst = fnCreateIl ();
173 protected override Expr VisitCompareLessThan (ExprCompareLessThan e)
175 return this.VisitBinary (e, () => this.il.Create (e.IsSigned ? OpCodes.Clt : OpCodes.Clt_Un));
178 protected override Expr VisitCompareGreaterThan (ExprCompareGreaterThan e)
180 return this.VisitBinary (e, () => this.il.Create (e.IsSigned ? OpCodes.Cgt : OpCodes.Cgt_Un));
183 protected override Expr VisitCompareEqual (ExprCompareEqual e)
185 return this.VisitBinary (e, () => this.il.Create (OpCodes.Ceq));
188 protected override Expr VisitAdd (ExprAdd e)
190 return this.VisitBinary (e, () => {
192 return this.il.Create (OpCodes.Add);
194 return this.il.Create (e.IsSigned ? OpCodes.Add_Ovf : OpCodes.Add_Ovf_Un);
199 protected override Expr VisitSub (ExprSub e)
201 return this.VisitBinary (e, () => {
203 return this.il.Create (OpCodes.Sub);
205 return this.il.Create (e.IsSigned ? OpCodes.Sub_Ovf : OpCodes.Sub_Ovf_Un);
210 protected override Expr VisitCall (ExprCall e)
212 foreach (var param in e.Parameters) {
215 var instCall = this.il.Create (OpCodes.Call, e.Method);
216 this.Emit (e, instCall);
220 protected override Expr VisitReturn (ExprReturn e)
222 var instReturn = this.il.Create (OpCodes.Ret);
223 this.Emit (e, instReturn);
227 protected override Expr VisitBox (ExprBox e)
229 this.Visit (e.ExprToBox);
230 var instBox = this.il.Create (OpCodes.Box, e.ReturnType);
231 this.Emit (e, instBox);
235 protected override Expr VisitConv (ExprConv e)
237 this.Visit (e.ExprToConvert);
238 Instruction instConv;
239 switch (e.ConvToType) {
241 instConv = this.il.Create (OpCodes.Conv_I4);
244 instConv = this.il.Create (OpCodes.Conv_I8);
247 throw new NotSupportedException ("Cannot conv to: " + e.ConvToType);
249 this.Emit (e, instConv);