The StaDyn Programming Language
Source code documentation of StaDyn, a hybrid static and dynamic typing language.
CLRCodeGenerator.cs
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
3 // Project rROTOR
4 // --------------------------------------------------------------------------
5 // File: CLRCodeGenerator.cs
6 // Author: Francisco Ortin - francisco.ortin@gmail.com
7 // Description:
8 // This class encapsulates the IL CLR code generator IL.
9 // Inheritance: CodeGenerator
10 // Implements Factory method [Abstract Product].
11 // --------------------------------------------------------------------------
12 // Create date: 21-08-2007
13 // Modification date: 21-08-2007
15 
16 using System;
17 using System.Collections.Generic;
18 using System.IO;
19 using System.Text;
20 
21 using TypeSystem;
22 
23 namespace CodeGeneration {
28  #region Constructor
29  public CLRCodeGenerator(TextWriter writer)
34  : base(writer) {
35 
36  }
37  #endregion
38 
39  #region UnboxAny
40  // Unbox <token>
41  // Revert a boxed value type instance from the object form to its value type form. <token> specifies the value type being converted and must be a valid TypeDef or TypeRef token.
42  // This instruction takes an object reference from the stack and puts a managed pointer to the value type instance on the stack.
43 
49  public override void UnboxAny(int indent, TypeExpression type) {
50  if(type is UnionType)
51  this.UnboxAny(indent, type as UnionType);
52  else if (!(type is TypeVariable))
53  this.WriteLNUnaryCommand(indent, "unbox.any", type.ILType());
54  else if(((TypeVariable)type).Substitution != null)
55  this.WriteLNUnaryCommand(indent, "unbox.any", type.ILType());
56  }
57 
58  private void UnboxAny(int indent, UnionType type)
59  {
60  String finalLabel = this.NewLabel;
61  String nextLabel = "";
62  for (int i = 0; i < type.TypeSet.Count; i++)
63  {
64  if (!String.IsNullOrEmpty(nextLabel))
65  this.WriteLabel(indent, nextLabel);
66  if (i != type.TypeSet.Count - 1)
67  {
68  nextLabel = this.NewLabel;
69  this.dup(indent);
70  this.isinst(indent, type.TypeSet[i]);
71  this.brfalse(indent, nextLabel);
72  }
73  this.WriteLNUnaryCommand(indent, "unbox.any", type.TypeSet[i].ILType());
74  if (i != type.TypeSet.Count - 1)
75  this.br(indent, finalLabel);
76  }
77  this.WriteLabel(indent, finalLabel);
78  }
79  #endregion
80 
81  #region CallVirt <token>
82  // CallVirt <token>
83  // Call the virtual method specified by <token>.
84 
93  public override void CallVirt(int indent, MethodType memberType, TypeExpression klass, string member, AST.CompoundExpression arguments) {
94  this.ilStamentsCodeGeneration.WriteIndentation(indent);
95  this.ilStamentsCodeGeneration.Write("callvirt\t");
96  this.WriteCall(memberType, klass, member, null);
97  this.ilStamentsCodeGeneration.WriteLine();
98  }
99  #endregion
100  }
101 }
Representa a union type.
Definition: UnionType.cs:36
override void UnboxAny(int indent, TypeExpression type)
Writes the Unbox and ldobj instruction (Unbox.any = Unbox + ldobj)
override string ILType()
Gets the type name to use in IL code.
override void WriteLabel(int indentation, string label)
Writes a label in il. Format –indentation– LABEL:
Abstract class that represents all different types.
Represents a generic type expression
Definition: TypeVariable.cs:36
This class encapsulates the IL CLR code generator IL.
CLRCodeGenerator(TextWriter writer)
Constructor of CodeGenerator.
virtual string ILType()
Gets the type name to use in IL code.
override void CallVirt(int indent, MethodType memberType, TypeExpression klass, string member, AST.CompoundExpression arguments)
Writes the CallVirt instruction.
Representa a method type.
Definition: MethodType.cs:37
IList< TypeExpression > TypeSet
Gets the list of type expressions
Definition: UnionType.cs:57