public override void GenerateCode(CodeGeneration.CodeGenerator cg) { base.GenerateCode(cg); Label return0 = cg.IlGenerator.DefineLabel(); Label end = cg.IlGenerator.DefineLabel(); if (LeftOperand.ReturnType == TypeInfo.Int) { //code gen for integer eq cg.IlGenerator.Emit(OpCodes.Ceq); return; } if (LeftOperand.ReturnType == TypeInfo.String) { //code gen for string eq MethodInfo stringeq = typeof(String).GetMethod("Equals", new[] { typeof(string), typeof(string) }); cg.IlGenerator.Emit(OpCodes.Call, stringeq); } else { //code gen for ref eq MethodInfo refeq = typeof(object).GetMethod("ReferenceEquals", new[] { typeof(object), typeof(object) }); cg.IlGenerator.Emit(OpCodes.Call, refeq); } cg.IlGenerator.Emit(OpCodes.Brfalse, return0); cg.IlGenerator.Emit(OpCodes.Ldc_I4_1); cg.IlGenerator.Emit(OpCodes.Br, end); cg.IlGenerator.MarkLabel(return0); cg.IlGenerator.Emit(OpCodes.Ldc_I4_0); cg.IlGenerator.MarkLabel(end); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { Label beginoffor = cg.IlGenerator.DefineLabel(); EndofCicle = cg.IlGenerator.DefineLabel(); //define the iteration variable and hook it with it's varinfo loopVariable.ILLocalVariable = cg.CreateTigerVar(typeof(int)); LocalBuilder upperbound = cg.IlGenerator.DeclareLocal(typeof(int)); //starting th generation LoopVariableInitExpression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stsfld, loopVariable.ILLocalVariable); LoopVariableUpperLimitExpression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stloc, upperbound); //mark the begin of the cicle cg.IlGenerator.MarkLabel(beginoffor); //do the comparison cg.IlGenerator.Emit(OpCodes.Ldsfld, loopVariable.ILLocalVariable); cg.IlGenerator.Emit(OpCodes.Ldloc, upperbound); cg.IlGenerator.Emit(OpCodes.Bgt, EndofCicle); //Generate the body DoExpression.GenerateCode(cg); if (BodyReturnsValue) { cg.IlGenerator.Emit(OpCodes.Pop); } //Incresing the counter cg.IlGenerator.Emit(OpCodes.Ldsfld, loopVariable.ILLocalVariable); cg.IlGenerator.Emit(OpCodes.Ldc_I4_1); cg.IlGenerator.Emit(OpCodes.Add); cg.IlGenerator.Emit(OpCodes.Stsfld, loopVariable.ILLocalVariable); cg.IlGenerator.Emit(OpCodes.Br, beginoffor); cg.IlGenerator.MarkLabel(EndofCicle); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { var varinfo = (VariableInfo)(LValue.MainIDNode.ReferencedThing); //si es una var sencilla genero código para Expression y asigno al ILLocal if (LValue.Children.Count == 1) { Expression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stsfld, varinfo.ILLocalVariable); } else { //se carga el obj o arr cg.IlGenerator.Emit(OpCodes.Ldsfld, varinfo.ILLocalVariable); TypeInfo resolutedtype = varinfo.VariableType; for (int i = 1; i < LValue.Children.Count; i++) { if (LValue.Children[i] is IndexingNode) { var node = (IndexingNode)LValue.Children[i]; Type targettype = (TypeInfo.ArrayFromTypeInfo(resolutedtype)).TargetType.GetILType(); node.IndexNode.GenerateCode(cg); if (i == LValue.Children.Count - 1) { //almacenar Expression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stelem, targettype); } else //si es arr se accede { cg.IlGenerator.Emit(OpCodes.Ldelem, targettype); resolutedtype = (TypeInfo.ArrayFromTypeInfo(resolutedtype)).TargetType; } } else { var membername = ((DotNode)LValue.Children[i]).MemberName; //resolver el field builder asoc al member var fieldbuilder = (TypeInfo.RecordFromTypeInfo(resolutedtype)).FieldBuilders[membername]; if (i == LValue.Children.Count - 1) { //almacenar Expression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stfld, fieldbuilder); } else { //actualizar el resoluted type info resolutedtype = (TypeInfo.RecordFromTypeInfo(resolutedtype)).Fields[membername]; cg.IlGenerator.Emit(OpCodes.Ldfld, fieldbuilder); } } } } }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { Label endofif = cg.IlGenerator.DefineLabel(); IfCondition.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Brfalse, endofif); ThenExpression.GenerateCode(cg); cg.IlGenerator.MarkLabel(endofif); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { var varinfo = (VariableInfo)((IdNode)VariableID).ReferencedThing; varinfo.ILLocalVariable = cg.CreateTigerVar(varinfo.VariableType.GetILType()); // varinfo.IsHidingAnother = false; InitialValue.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stsfld, varinfo.ILLocalVariable); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { var recordinfo = TypeInfo.RecordFromTypeInfo(Scope.ResolveType(TypeNode.Text)); //Generate code for the initialization expr foreach (ASTNode initializationExpression in InitializationExpressions) { initializationExpression.GenerateCode(cg); } //Creates the new obj cg.IlGenerator.Emit(OpCodes.Newobj, recordinfo.Constructor); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { //definir los tipos y enlazarlos con su builder menos los alias, a esos le daremos un trato especial var typenames = from typedec in Children where typedec is RecordDeclarationNode select((TypeDeclarationNode)typedec).NewTypeNode.TypeName; var typesinfo = from name in typenames select Scope.ResolveType(name); foreach (var typeInfo in typesinfo) { typeInfo.ILTypeBuilder = cg.CreateType(); } base.GenerateCode(cg); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { foreach (FunctionDeclarationNode declarationNode in Children) { var funcinfo = (FunctionInfo)declarationNode.FunctionIDNode.ReferencedThing; var parameterstypes = (from paramstypeinfos in funcinfo.Parameters.Values select paramstypeinfos.GetILType()).ToArray(); //create the function in the module funcinfo.ILMethod = cg.CreateFunction(declarationNode.FunctionReturnType.GetILType(), parameterstypes); } base.GenerateCode(cg); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { Label beginofwhile = cg.IlGenerator.DefineLabel(); EndofCicle = cg.IlGenerator.DefineLabel(); cg.IlGenerator.MarkLabel(beginofwhile); ConditionExpression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Brfalse, EndofCicle); BodyExpression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Br, beginofwhile); cg.IlGenerator.MarkLabel(EndofCicle); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { if (Children != null) { for (int i = 0; i < ChildCount - 1; i++) { var child = Children[i] as ASTNode; child.GenerateCode(cg); if (child.ReturnType != TypeInfo.Void) { cg.IlGenerator.Emit(OpCodes.Pop); } } (Children[ChildCount - 1] as ASTNode).GenerateCode(cg); } }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { var arrayinfo = TypeInfo.ArrayFromTypeInfo(Scope.ResolveType(TypeNode.TypeName)); //to get the length LocalBuilder length = cg.IlGenerator.DeclareLocal(typeof(int)); LocalBuilder count = cg.IlGenerator.DeclareLocal(typeof(int)); LocalBuilder arr = cg.IlGenerator.DeclareLocal(arrayinfo.GetILType()); Label initloopbegin = cg.IlGenerator.DefineLabel(); Label initloopend = cg.IlGenerator.DefineLabel(); //set the count to zero cg.IlGenerator.Emit(OpCodes.Ldc_I4_0); cg.IlGenerator.Emit(OpCodes.Stloc, count); LengthExpression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Dup); //get the length cg.IlGenerator.Emit(OpCodes.Stloc, length); cg.IlGenerator.Emit(OpCodes.Newarr, arrayinfo.TargetType.GetILType()); cg.IlGenerator.Emit(OpCodes.Stloc, arr); //make the initialization loop cg.IlGenerator.MarkLabel(initloopbegin); //make the comparison and jmp to end if does not hold cg.IlGenerator.Emit(OpCodes.Ldloc, count); cg.IlGenerator.Emit(OpCodes.Ldloc, length); cg.IlGenerator.Emit(OpCodes.Bge, initloopend); //Evaluate the expr and store it cg.IlGenerator.Emit(OpCodes.Ldloc, arr); cg.IlGenerator.Emit(OpCodes.Ldloc, count); InitExpression.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Stelem, arrayinfo.TargetType.GetILType()); //increase count and jmp to begin cg.IlGenerator.Emit(OpCodes.Ldloc, count); cg.IlGenerator.Emit(OpCodes.Ldc_I4_1); cg.IlGenerator.Emit(OpCodes.Add); cg.IlGenerator.Emit(OpCodes.Stloc, count); cg.IlGenerator.Emit(OpCodes.Br, initloopbegin); cg.IlGenerator.MarkLabel(initloopend); //leave the arr reference in the top of the stack cg.IlGenerator.Emit(OpCodes.Ldloc, arr); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { base.GenerateCode(cg); //not whatever the equals gives MethodBuilder not = ((FunctionInfo)Semantic.Scope.DefaultGlobalScope.ResolveVarOrFunction("not")).ILMethod; cg.IlGenerator.Emit(OpCodes.Call, not); #region A mano //Label return0 = cg.IlGenerator.DefineLabel(); //Label end = cg.IlGenerator.DefineLabel(); ////if there was 1 ret 0 else ret 1 //cg.IlGenerator.Emit(OpCodes.Brtrue,return0); //cg.IlGenerator.Emit(OpCodes.Ldc_I4_1); //cg.IlGenerator.Emit(OpCodes.Br, end); //cg.IlGenerator.MarkLabel(return0); //cg.IlGenerator.Emit(OpCodes.Ldc_I4_0); //cg.IlGenerator.MarkLabel(end); #endregion }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { Label rettrue = cg.IlGenerator.DefineLabel(); Label end = cg.IlGenerator.DefineLabel(); LeftOperand.GenerateCode(cg); //if eval its 1 then ret 1 cg.IlGenerator.Emit(OpCodes.Brtrue, rettrue); RightOperand.GenerateCode(cg); //if eval its 1 then ret 1 cg.IlGenerator.Emit(OpCodes.Brtrue, rettrue); //ret 0 and jump to end cg.IlGenerator.Emit(OpCodes.Ldc_I4_0); cg.IlGenerator.Emit(OpCodes.Br, end); //ret 0 cg.IlGenerator.MarkLabel(rettrue); cg.IlGenerator.Emit(OpCodes.Ldc_I4_1); cg.IlGenerator.MarkLabel(end); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { base.GenerateCode(cg); if (LeftOperand.ReturnType == TypeInfo.Int) { cg.IlGenerator.Emit(OpCodes.Cgt); } else { Label ret1 = cg.IlGenerator.DefineLabel(); Label end = cg.IlGenerator.DefineLabel(); MethodInfo strcmp = typeof(string).GetMethod("CompareOrdinal", new[] { typeof(string), typeof(string) }); //cmp with zero, if greater return 1 cg.IlGenerator.Emit(OpCodes.Call, strcmp); cg.IlGenerator.Emit(OpCodes.Ldc_I4_0); cg.IlGenerator.Emit(OpCodes.Bgt, ret1); cg.IlGenerator.Emit(OpCodes.Ldc_I4_0); cg.IlGenerator.Emit(OpCodes.Br, end); cg.IlGenerator.MarkLabel(ret1); cg.IlGenerator.Emit(OpCodes.Ldc_I4_1); cg.IlGenerator.MarkLabel(end); } }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { base.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Ldc_I4, int.Parse(Text)); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { base.GenerateCode(cg); cg.IlGenerator.Emit(OpCodes.Sub); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { cg.IlGenerator.BeginScope(); base.GenerateCode(cg); cg.IlGenerator.EndScope(); }
public override void GenerateCode(CodeGeneration.CodeGenerator cg) { cg.IlGenerator.Emit(OpCodes.Br, EnclosingForOrWhile.EndofCicle); }