public virtual void GenSimple(CodeGenContext context) { context.ldloc(0); context.ldfld(field); }
internal override void GenCode0(CodeGenContext context) { // String.Concat(String.Concat(arg1, arg2), args, ...); head.GenCode0(context); if (head.nd_next != null) { int first = context.StoreInTemp("head", Runtime.StringRef, head.location); for (Node n = head.nd_next; n != null; n = n.nd_next) { n.GenCode0(context); int second = context.StoreInTemp("tail", Runtime.StringRef, n.location); context.ldloc(first); context.ldloc(second); context.callvirt(Runtime.String.Concat); context.stloc(first); context.ReleaseLocal(second, true); } context.ldloc(first); context.ReleaseLocal(first, true); } }
internal override void GenCode0(CodeGenContext context) { // hash = new Hash(); context.newobj(Runtime.Hash.ctor); int hash = context.StoreInTemp("hash", Runtime.HashRef, location); Node entry = elements; while (entry != null) { bool key_created, value_created; ISimple key = context.PreCompute0(entry, "key", out key_created); entry = entry.nd_next; ISimple value = context.PreCompute0(entry, "value", out value_created); entry = entry.nd_next; // hash.Add(key, value); context.ldloc(hash); key.GenSimple(context); value.GenSimple(context); context.callvirt(Runtime.Hash.Add); context.ReleaseLocal(key, key_created); context.ReleaseLocal(value, value_created); } context.ldloc(hash); context.ReleaseLocal(hash, true); }
protected void ReturnArray(CodeGenContext context) { // Ruby.Eval.Return(array, caller); array.GenCode(context); context.ldloc(0); context.call(Runtime.Eval.Return); }
internal void GenCall(CodeGenContext context) { int result = context.CreateLocal("result", PrimitiveType.Object); PERWAPI.CILLabel endLabel = context.NewLabel(); PERWAPI.CILLabel retryLabel = context.NewLabel(); context.CodeLabel(retryLabel); context.StartBlock(Clause.Try); { // object result = Call(...) GenCall0(context); context.stloc(result); context.Goto(endLabel); } PERWAPI.TryBlock tryBlock = context.EndTryBlock(); context.StartBlock(Clause.Catch); { CatchBreakException(context, result, endLabel); } context.EndCatchBlock(Runtime.BreakExceptionRef, tryBlock); context.StartBlock(Clause.Catch); { CatchRetryException(context, retryLabel); } context.EndCatchBlock(Runtime.RetryExceptionRef, tryBlock); context.CodeLabel(endLabel); context.ldloc(result); context.ReleaseLocal(result, true); }
internal override void GenCode0(CodeGenContext context) { PERWAPI.CILLabel finalLabel = context.NewLabel(); int RescueTemp = context.CreateLocal("rescueTemp", PERWAPI.PrimitiveType.Object); context.ldnull(); context.stloc(RescueTemp); if (ensure != null) { context.StartBlock(Clause.Try); // outer try block with finally context.StartBlock(Clause.Try); // inner try block with catch } GenInnerBlock(context, RescueTemp); if (ensure != null) { context.Goto(finalLabel); PERWAPI.TryBlock innerTry = context.EndTryBlock(); context.StartBlock(Clause.Catch); GenRescue(context, null, 0, null); context.EndCatchBlock(Runtime.SystemExceptionRef, innerTry); PERWAPI.TryBlock outerTry = context.EndTryBlock(); // Fixme: reset labels to prevent branches out of finally block. context.StartBlock(Clause.Finally); ensure.GenCode(context); if (context.Reachable()) context.pop(); context.endfinally(); context.EndFinallyBlock(outerTry); context.CodeLabel(finalLabel); context.newEndPoint(location); } context.ldloc(RescueTemp); context.ReleaseLocal(RescueTemp, true); }
internal void CopySimple(CodeGenContext context, Scope scope) { if (block != null) { // locals.block = block; string bname = ID.ToDotNetName(block.vid); context.ldloc(0); LoadBlock(context); context.stfld(scope.GetFrameField(bname)); } for (Node f = normal; f != null; f = f.nd_next) { string fname = ID.ToDotNetName(((VAR)f).vid); // local.f = f; context.ldloc(0); context.ldarg(fname); context.stfld(scope.GetFrameField(fname)); } }
internal override void GenCode0(CodeGenContext context) { if (qualified) if (scope != null) scope.GenCode(context); else context.ldsfld(Ruby.Compiler.Runtime.Init.rb_cObject); else context.ruby_cbase(parent_scope); context.ldstr(vid.ToString()); context.ldloc(0); context.call(Runtime.Eval.get_const); }
internal override void Defined(CodeGenContext context) { if (qualified) if (scope != null) { // object result; int result = context.CreateLocal("result", PrimitiveType.Object); PERWAPI.CILLabel endLabel = context.NewLabel(); // try { context.StartBlock(Clause.Try); { // result = Eval.const_defined(scope, vid, caller); scope.GenCode(context); context.ldstr(vid.ToString()); context.ldloc(0); context.call(Runtime.Eval.const_defined); context.stloc(result); context.leave(endLabel); } TryBlock block = context.EndTryBlock(); // catch (System.Exception) { context.StartBlock(Clause.Catch); { // result = null; context.ldnull(); context.stloc(result); context.leave(endLabel); } context.EndCatchBlock(Runtime.SystemExceptionRef, block); context.CodeLabel(endLabel); context.ldloc(result); context.ReleaseLocal(result, true); } else { context.ldsfld(Ruby.Compiler.Runtime.Init.rb_cObject); context.ldstr(vid.ToString()); context.ldloc(0); context.call(Runtime.Eval.const_defined); } else { context.ruby_cbase(parent_scope); context.ldstr(vid.ToString()); context.ldloc(0); context.call(Runtime.Eval.const_defined); } }
internal void AddScopeBody(CodeGenContext context) { returnTemp = context.CreateLocal("returnTemp", PrimitiveType.Object); context.labels = new Labels(); context.labels.Redo = context.NewLabel(); context.labels.Return = context.NewLabel(); // try { ... } context.StartBlock(Clause.Try); { if (BEGIN != null) BEGIN.GenCode(context); context.CodeLabel(context.labels.Redo); if (body != null) { body.GenCode(context); if (context.Reachable()) context.stloc(returnTemp); } context.Goto(context.labels.Return); } PERWAPI.TryBlock tryBlock = context.EndTryBlock(); CatchReturnException(context, tryBlock); // ReturnLabel: // return returnTemp; context.CodeLabel(context.labels.Return); context.newEndPoint(location); if (context.Method.GetRetType() != PERWAPI.PrimitiveType.Void) context.ldloc(returnTemp); context.ret(); context.ReleaseLocal(returnTemp, true); }
internal override void GenCode0(CodeGenContext context) { CodeGenContext Begin = context.CreateMethod(FileClass(), MethAttr.PublicStatic, "Begin" + (seq++), PrimitiveType.Object, new Param(ParamAttr.Default, "recv", PrimitiveType.Object), new Param(ParamAttr.Default, "caller", Runtime.FrameRef)); Begin.startMethod(this.location); AddScopeLocals(Begin); AddScopeBody(Begin); Begin.ReleaseLocal(0, true); Begin.Close(); // Begin(recv, caller); context.ldarg("recv"); context.ldloc(0); context.call(Begin.Method); context.pop(); }
internal override void GenCode0(CodeGenContext context) { int receiverLocal = 0; // singleton_class(caller, receiver).define_method(...); context.newStartPoint(location); if (!(receiver is AST.CALL)) { context.ldloc(0); receiver.GenCode(context); } else { receiver.GenCode(context); receiverLocal = context.CreateLocal("receiverLocal", PrimitiveType.Object); context.stloc(receiverLocal); context.ldloc(0); context.ldloc(receiverLocal); } context.call(Runtime.Class.singleton_class); DefineMethod(context); if (receiver is AST.CALL) context.ReleaseLocal(receiverLocal, true); }
internal override void Defined(CodeGenContext context) { CILLabel endLabel = context.NewLabel(); context.ldloc(0); context.call(Runtime.Frame.get_Tilde); context.dup(); context.brfalse(endLabel); context.ldc_i4(nth); context.callvirt(Runtime.Match.defined_nth); context.CodeLabel(endLabel); }
internal override void Assign(CodeGenContext context, Node rhs) { // Fixme: make sure CurrentRubyClass is not a singleton (see cvar_cbase) // object value = rhs; bool created; ISimple value = context.PreCompute(rhs, "rhs", out created); // Ruby.Variables.cvar_set(caller, ruby_cref, "vid", value); context.ldloc(0); context.ruby_cbase(parent_scope); context.ldstr(vid.ToString()); value.GenSimple(context); context.call(Runtime.Variables.cvar_set); context.ReleaseLocal(value, created); }
internal override void Assign(CodeGenContext context, Node rhs) { bool value_created; ISimple value; if (qualified) // Fixme: scope == null??? { // object where = scope; bool where_created; ISimple where = context.PreCompute(scope, "scope", out where_created); // object value = rhs; value = context.PreCompute(rhs, "rhs", out value_created); context.ldloc(0); where.GenSimple(context); context.ReleaseLocal(where, where_created); } else { // object value = rhs; value = context.PreCompute(rhs, "rhs", out value_created); context.ldloc(0); context.ruby_cbase(parent_scope); } context.ldstr(vid.ToString()); value.GenSimple(context); context.call(Runtime.Eval.set_const); context.ReleaseLocal(value, value_created); }
internal override void GenCode0(CodeGenContext context) { // Eval.alias(ruby_class, lhs, rhs, myFrame); context.newLine(location); context.ruby_class(parent_scope); context.ldstr(lhs.ToString()); context.ldstr(rhs.ToString()); context.ldloc(0); context.call(Runtime.Eval.alias); }
internal override void GenCode0(CodeGenContext context) { bool scope_created, super_created; CodeGenContext newContext = new CodeGenContext(context); // scope and super must be evaluated in surrounding scope and passed to Init method ISimple scope = PushScope(context, out scope_created); ISimple super = PushSuper(context, out super_created); string basename = ID.ToDotNetName(name.vid); // BBTAG: try and get the superclass PERWAPI.Class superClass = Runtime.ObjectRef; bool superClassFound = false; PERWAPI.Method superClassConstructor0 = null; PERWAPI.Method superClassConstructor1 = Runtime.Object.ctor; if (this is CLASS) { superClass = ((CLASS)this).GetSuperClassRef(newContext); if (superClass != null) { superClassFound = true; superClassConstructor0 = superClass.GetMethodDesc(".ctor", new Type[0]); if (superClassConstructor0 is MethodDef) superClassConstructor0 = ((MethodDef)superClassConstructor0).MakeRefOf(); superClassConstructor1 = superClass.GetMethodDesc(".ctor", new Type[] { Runtime.ClassRef }); if (superClassConstructor1 is MethodDef) superClassConstructor1 = ((MethodDef)superClassConstructor1).MakeRefOf(); } else superClass = Runtime.ObjectRef; } //public class MyClass: Object { // check if this class has already been created in a referenced DLL // FIXME: this won't work for nested classes PERWAPI.Class classRef = ClassSkeleton.FindPERWAPIClass(null, name, context.peFiles); bool defineInteropClass = (classRef == null); // if it seems like an extension of a ruby builtin class, don't generate an interop class // FIXME: this won't work for nested classes if (Ruby.Runtime.BuiltinClasses.IsBuiltinClass(basename)) { Compiler.InteropWarning(basename + " is a Ruby built-in class, interop class not generated"); defineInteropClass = false; } //ClassDef interopClass = newContext.CreateNestedClass(CurrentInteropClass(), basename, Runtime.ObjectRef); // BBTAG ClassDef interopClass = null; if (defineInteropClass) { if (superClass is PERWAPI.ClassDef) interopClass = newContext.CreateNestedClass(CurrentInteropClass(), basename, ((PERWAPI.ClassDef)superClass).MakeRefOf()); // BBTAG else interopClass = newContext.CreateNestedClass(CurrentInteropClass(), basename, superClass); // BBTAG interopClasses.Push(interopClass); classRef = interopClass; } //context.classes[classname] = interopClass; // BBTAG: create skeleton for this class ClassSkeleton skeleton; if (context.currentSkeleton.nestedClasses.ContainsKey(basename)) skeleton = context.currentSkeleton.nestedClasses[basename]; else skeleton = new ClassSkeleton(basename, classRef); skeleton.lexicalParent = context.currentSkeleton; newContext.currentSkeleton = skeleton; skeleton.lexicalParent.nestedClasses[basename] = skeleton; if (!superClassFound && this is AST.CLASS && ((AST.CLASS)this).super_class != null && defineInteropClass) { // do not add to post pass list if supertype is a built-in Ruby class //List<string> qualifiedSuperClass = ClassSkeleton.ConstNodeToClassList(((AST.CLASS)this).super_class); //if (!(qualifiedSuperClass.Count == 1 && Ruby.Runtime.BuiltinClasses.IsBuiltinClass(qualifiedSuperClass[0]))) newContext.postPassList.Add(new ClassSkeletonPostPass(skeleton, interopClass, ((AST.CLASS)this).super_class)); } // public static Class classname; int seqNo = 0; internal_name = basename; while (FileClass().GetField(internal_name) != null) internal_name = basename + (seqNo++); // Define singleton in file class so that it gets initialized by .cctor singletonField = FileClass().AddField(PERWAPI.FieldAttr.PublicStatic, internal_name, Runtime.ClassRef); newContext.CurrentRubyClass = singletonField; // public MyClass() : base(singleton) { }; CodeGenContext class_constructor = null; if (defineInteropClass) { if (interopClass.GetMethod(".ctor", new Type[0]) == null) { CodeGenContext class_constructor0 = newContext.CreateConstructor(interopClass); class_constructor0.ldarg(0); if (superClassConstructor0 != null) class_constructor0.call(superClassConstructor0); else { class_constructor0.ldsfld(singletonField); class_constructor0.call(superClassConstructor1); } class_constructor0.ret(); class_constructor0.Close(); } // public MyClass(Class klass) : base(klass) { }; MethodDef ctor = interopClass.GetMethod(".ctor", new Type[] { Runtime.ClassRef }); //CodeGenContext class_constructor; if (ctor == null) { class_constructor = newContext.CreateConstructor(interopClass, new Param(ParamAttr.Default, "klass", Runtime.ClassRef)); class_constructor.ldarg(0); class_constructor.ldarg("klass"); class_constructor.call(superClassConstructor1); class_constructor.ret(); class_constructor.Close(); } else { class_constructor = new CodeGenContext(); class_constructor.Method = ctor; } } // internal static void Init_fullname(object scope, object super, object recv, Frame caller) { CodeGenContext Init = newContext.CreateMethod(FileClass(), MethAttr.PublicStatic, "Init_" + internal_name, PrimitiveType.Object, new Param(ParamAttr.Default, "scope", PrimitiveType.Object), new Param(ParamAttr.Default, "super", PrimitiveType.Object), new Param(ParamAttr.Default, "recv", PrimitiveType.Object), new Param(ParamAttr.Default, "caller", Runtime.FrameRef)); skeleton.initMethod = Init.Method; Init.startMethod(this.location); AddScopeLocals(Init); // singleton = scope.define_???(...) DefineClass(Init, singletonField); // recv = singleton; Init.ldsfld(singletonField); Init.starg("recv"); // Fixme: should be conditional // singleton.define_allocator(allocator); if (defineInteropClass) { FieldDef allocator = DefineAllocator(newContext, class_constructor.Method); if (allocator != null) { Init.ldsfld(singletonField); Init.ldsfld(allocator); Init.call(Runtime.Class.define_alloc_func); } } AddScopeBody(Init); Init.ReleaseLocal(0, true); Init.Close(); if (defineInteropClass) interopClasses.Pop(); // --------------------- Return to old Context ---------------------------- context.newLine(location); // Init(scope, super, recv, caller); scope.GenSimple(context); super.GenSimple(context); context.ldarg("recv"); context.ldloc(0); context.call(Init.Method); context.ReleaseLocal(super, super_created); context.ReleaseLocal(scope, scope_created); }
internal override void DefineClass(CodeGenContext context, PERWAPI.FieldDef singleton) { // Class.define_module(scope, name.vid, caller); context.ldarg("scope"); context.ldstr(name.vid.ToString()); context.ldloc(0); context.call(Runtime.Class.rb_define_module); context.stsfld(singleton); }
public void GenSimple(CodeGenContext context) { // Fixme: make sure CurrentRubyClass is not a singleton (see cvar_cbase) // Ruby.Variables.cvar_get(caller, ruby_cref, "vid"); context.ldloc(0); context.ruby_cbase(parent_scope); context.ldstr(vid.ToString()); context.call(Runtime.Variables.cvar_get); }
internal override void Assign(CodeGenContext context, Node rhs) { // object value = rhs; bool created; ISimple value = context.PreCompute(rhs, "rhs", out created); // locals.field = value context.ldloc(0); value.GenSimple(context); context.stfld(field); GenCode0(context); context.ReleaseLocal(value, created); }
internal override void GenCode0(CodeGenContext context) { context.ldloc(0); context.call(Runtime.Frame.get_Tilde); PERWAPI.CILLabel label1 = context.NewLabel(); context.brfalse(label1); context.ldloc(0); context.call(Runtime.Frame.get_Tilde); context.ldloc(0); switch (ch) { case '&': context.callvirt(Runtime.Match.last_match); break; case '`': context.callvirt(Runtime.Match.match_pre); break; case '\'': context.callvirt(Runtime.Match.match_post); break; case '+': context.callvirt(Runtime.Match.match_last); break; default: throw new NotImplementedException("BACK_REF $" + ch); } PERWAPI.CILLabel label2 = context.NewLabel(); context.br(label2); context.CodeLabel(label1); context.ldnull(); context.CodeLabel(label2); }
public override void GenSimple(CodeGenContext context) { // frame.GetDynamic("vid"); context.ldloc(0); context.ldstr(Name); context.call(Runtime.Frame.GetDynamic); }
internal void DefineMethod(CodeGenContext context) { // ... .define_method("MyMethod", MyMethod.singleton, arity, caller); context.ldstr(method_id.ToString()); context.ldsfld(GenerateClassForMethod(context)); context.ldc_i4(formals.arity); context.ldloc(0); context.callvirt(Runtime.Class.define_method); context.ldnull(); }
internal override void Assign(CodeGenContext context, Node rhs) { // object value = rhs; bool created; ISimple value = context.PreCompute(rhs, "rhs", out created); // frame.SetDynamic("vid", value); context.ldloc(0); context.ldstr(Name); value.GenSimple(context); context.call(Runtime.Frame.SetDynamic); value.GenSimple(context); context.ReleaseLocal(value, created); }
internal override void GenCode0(CodeGenContext context) { MethodDef BlockN_constructor = GenerateClassForMethod(context); // new Ruby.Proc(recv, block, new BlockN(locals, this.locals1, this.locals2 ...), arity); context.ldarg("recv"); // recv LoadBlock(context); context.ldloc(0); // locals if (block_parent is BLOCK) foreach (FieldDef field in ((BLOCK)block_parent).frameFields) { // this.localsN context.ldarg(0); context.ldfld(field); } context.newobj(BlockN_constructor); context.ldc_i4(arity); context.newobj(Runtime.Proc.ctor); }
public void GenSimple(CodeGenContext context) { // Variables.gvar_get(vid, caller); context.ldstr(vid.ToString()); context.ldloc(0); context.call(Runtime.Variables.gvar_get); }
internal void AddScopeLocals(CodeGenContext context) { // ------------------ Start new Context ---------------------------- // [InteropMethod("MyClass")] // private class ActivationFrame: Ruby.Frame { ... } frame_def = context.CreateGlobalClass("_Internal", "Frame" + (N++), Runtime.FrameRef); Scope parentClass; for (parentClass = this; parentClass != null && !(parentClass is CLASS_OR_MODULE); parentClass = parentClass.parent_scope) ; string className = ""; if (parentClass != null) className = ((CLASS_OR_MODULE)parentClass).internal_name; ClassDef fileClass = FileClass(); string src = ""; if (fileClass != null && fileClass.Name().StartsWith("SourceFile_")) { src = fileClass.Name().Substring(11); frame_def.AddCustomAttribute(Runtime.FrameAttribute.ctor, new Constant[] { new StringConst(src), new StringConst(className) }); } foreach (string local in locals_list) CodeGenContext.AddField(frame_def, PERWAPI.FieldAttr.Public, ID.ToDotNetName(local), PrimitiveType.Object); // internal ActivationFrame(Frame caller): base(caller) { } CodeGenContext frame_ctor = context.CreateConstructor(frame_def, new Param(ParamAttr.Default, "caller", Runtime.FrameRef)); frame_ctor.ldarg(0); frame_ctor.ldarg("caller"); frame_ctor.call(Runtime.Frame.ctor); frame_ctor.ret(); frame_ctor.Close(); // internal string file() { CodeGenContext file = context.CreateMethod(frame_def, PERWAPI.MethAttr.PublicVirtual, "file", PrimitiveType.String); file.startMethod(this.location); // return "thisfile.rb" file.ldstr(this.location.file); file.ret(); file.Close(); // internal override string methodName() { CodeGenContext methodName = context.CreateMethod(frame_def, PERWAPI.MethAttr.PublicVirtual, "methodName", PrimitiveType.String); methodName.startMethod(this.location); // return "CurrentMethodName" methodName.ldstr(CurrentMethodName()); methodName.ret(); methodName.Close(); CreateNestingMethod(frame_def, context); CreateLastClassMethod(frame_def, context); // ------------------ Return to Old Context ---------------------- // ActivationFrame frame = new ActivationFrame(caller); context.ldarg("caller"); context.newobj(frame_ctor.Method); int frame = context.StoreInTemp("frame", FrameClass, location); Debug.Assert(frame == 0); // frame.block_arg = block; context.ldloc(frame); LoadBlock0(context); context.stfld(Runtime.Frame.block_arg); if (this is BLOCK) { // frame.current_block = this; context.ldloc(frame); context.ldarg(0); context.stfld(Runtime.Frame.current_block); } }
internal override void Assign(CodeGenContext context, Node rhs) { bool created; ISimple value = context.PreCompute(rhs, "rhs", out created); //Ruby.Variables.gvar_set(vid, rhs, caller); context.ldstr(vid.ToString()); value.GenSimple(context); context.ldloc(0); context.call(Runtime.Variables.gvar_set); context.ReleaseLocal(value, created); }
internal void CatchReturnException(CodeGenContext context, PERWAPI.TryBlock tryBlock) { // catch (Ruby.ReturnException exception) { ... } context.StartBlock(Clause.Catch); { PERWAPI.CILLabel falseLabel = context.NewLabel(); int exception = context.StoreInTemp("exception", Runtime.ReturnExceptionRef, location); // if (exception.scope == thisframe) context.ldloc(exception); context.ldfld(Runtime.ReturnException.scope); context.ldloc(0); context.bne(falseLabel); // returnTemp = exception.return_value; context.ldloc(exception); context.ldfld(Runtime.ReturnException.return_value); context.stloc(returnTemp); context.Goto(context.labels.Return); // falseLabel: context.CodeLabel(falseLabel); // throw exception context.rethrow(); context.ReleaseLocal(exception, true); } context.EndCatchBlock(Runtime.ReturnExceptionRef, tryBlock); }
internal override void Assign(CodeGenContext context, Node rhs) { // object value = rhs; bool created; ISimple value = context.PreCompute(rhs, "rhs", out created); // Eval.ivar_set(caller, recv, "vid", rhs); context.ldloc(0); context.ldarg("recv"); context.ldstr(vid); value.GenSimple(context); context.call(Runtime.Eval.ivar_set); context.ReleaseLocal(value, created); }