Example #1
0
        public override void Generate(Generator generator)
        {
            rootConstructorNode.GenerateFunction(generator);
            generator.Resolver.EnterContext();
            generator.Resolver.CurrentFieldName = ConstructorName();
            generator.AllocateAssembler();
            parametersMetadata.Generate(generator);
            generator.Symbols.Source(generator.Assembler.Region.CurrentLocation, this);
            generator.Assembler.StartFunction();
            generator.Assembler.CallAllocator(generator.Allocator, ParentDefinition.InstanceSize, ParentDefinition.RuntimeStruct);
            int thisslot = generator.Resolver.ResolveSlotOffset(new Identifier(this, "this"));

            generator.Resolver.IncompleteSlot(thisslot, false);
            generator.Assembler.StoreVariable(thisslot);

            generator.Assembler.SetTypePart(rootConstructorNode.RuntimeStruct);
            generator.Assembler.PushValue();
            foreach (ParameterMetadata p in parametersMetadata.ParameterList)
            {
                generator.Assembler.RetrieveVariable(p.Slot);
                generator.Assembler.PushValue();
            }
            Placeholder retSite = generator.Assembler.CallFromStack(parametersMetadata.ParameterList.Count);

            generator.AddCallTraceEntry(retSite, this, generator.Resolver.CurrentDefinition.Name.DataModifierLess, generator.Resolver.CurrentFieldName);

            generator.Resolver.LeaveContext();
            generator.Assembler.RetrieveVariable(thisslot);

            generator.Assembler.StopFunction();
            generator.Symbols.Source(generator.Assembler.Region.CurrentLocation, this, SourceMark.EndSequence);
            generator.Symbols.WriteCode(generator.Assembler.Region.BaseLocation, generator.Assembler.Region.Length, "constructor:" + ParentDefinition.TypeReference.TypeName.Data + ".constructor");
            functionPointer = generator.Assembler.Region.BaseLocation;

            castFunction = new CallableCastFunction(this);
            castFunction.Generate(generator);
            if (ParentDefinition.GarbageCollectable)
            {
                runtimeStruct.WriteNumber(1);
            }
            else
            {
                runtimeStruct.WriteNumber(0);
            }
            runtimeStruct.WritePlaceholder(castFunction.FunctionPointer);
            runtimeStruct.WriteNumber(0);
            runtimeStruct.WriteNumber(0);
            runtimeStruct.WritePlaceholder(functionPointer);
            runtimeStruct.WritePlaceholder(ParentDefinition.RuntimeStruct);
            generator.Symbols.WriteData(runtimeStruct.BaseLocation, runtimeStruct.Length, "ms:" + ParentDefinition.TypeReference.TypeName.Data + ".constructor");
        }
Example #2
0
            public void MakeRuntimeStruct(Generator generator, Dictionary <Definition, Placeholder> implicitConversions)
            {
                CallableCastFunction castFunction = new CallableCastFunction(location);

                castFunction.Generate(generator);
                if (definition.GarbageCollectable)
                {
                    runtimeStruct.WriteNumber(1);
                }
                else
                {
                    runtimeStruct.WriteNumber(0);
                }
                runtimeStruct.WritePlaceholder(castFunction.FunctionPointer);
                runtimeStruct.WriteNumber(0);
                runtimeStruct.WriteNumber(0);
                runtimeStruct.WritePlaceholder(functionPointer);
                runtimeStruct.WritePlaceholder(implicitConversions[definition]);

                StringBuilder sb = new StringBuilder(256);

                sb.Append("cs:");
                sb.Append(definition.Name.Data);
                sb.Append(":");
                sb.Append(parentDefinition.Name.Data);
                sb.Append(".constructor");
                generator.Symbols.WriteData(runtimeStruct.BaseLocation, runtimeStruct.Length, sb.ToString());

                if (redirect != null)
                {
                    redirect.node.MakeRuntimeStruct(generator, implicitConversions);
                }
                else
                {
                    foreach (ConstructorNodeInvocation l in inherit)
                    {
                        l.node.MakeRuntimeStruct(generator, implicitConversions);
                    }
                }
            }
Example #3
0
        public override void Generate(Generator generator)
        {
            Require.False(generated);
            Require.True(prepared);
            generated    = true;
            castFunction = new CallableCastFunction(this);
            castFunction.Generate(generator);

            generator.Resolver.EnterContext();
            generator.Resolver.SetContextParameters(parametersMetadata);
            generator.Resolver.CurrentFieldName = name.Data + "()";

            if (modifiers.Extern)
            {
                string className = ParentDefinition.Name.PrimaryName.Data;
                if (className == "pluk.base.Array")
                {
                    string s = ParentDefinition.Name.Data;
                    if (s == "pluk.base.Array<pluk.base.Int>")
                    {
                        className = "pluk.base.Array..pluk.base.Int";
                    }
                    else if (s == "pluk.base.Array<pluk.base.Bool>")
                    {
                        className = "pluk.base.Array..pluk.base.Bool";
                    }
                    else if (s == "pluk.base.Array<pluk.base.Byte>")
                    {
                        className = "pluk.base.Array..pluk.base.Byte";
                    }
                }
                string fieldName = name.Data;
                if (modifiers.ExternMetadata == null)
                {
                    string namespaceName = ParentDefinition.Name.PrimaryName.Namespace;
                    generator.AllocateAssembler();
                    parametersMetadata.Generate(generator);
                    ParameterMetadata thisParam = parametersMetadata.Find("this");
                    generator.Resolver.SetImplicitFields(thisParam.Slot, thisParam.TypeReference);

                    generator.Symbols.Source(generator.Assembler.Region.CurrentLocation, this);
                    generator.Assembler.StartFunction();
                    generator.Assembler.SetupNativeReturnSpace();
                    int count = parametersMetadata.NativeArgumentCount();
                    generator.Assembler.SetupNativeStackFrameArgument(count);
                    parametersMetadata.WriteNative(generator.Assembler);
                    generator.Assembler.CallNative(generator.Importer.FetchImport(namespaceName, className, fieldName), count * 2, true, false);
                    generator.Assembler.StopFunction();
                    generator.Symbols.Source(generator.Assembler.Region.CurrentLocation, this, SourceMark.EndSequence);
                    generator.Symbols.WriteCode(generator.Assembler.Region.BaseLocation, generator.Assembler.Region.Length, "method-externed:" + ParentDefinition.Name.Data + "." + name.Data);
                    functionPointer = generator.Assembler.Region.BaseLocation;
                }
                else
                {
                    functionPointer = modifiers.ExternMetadata.Generate(generator, ParentDefinition.Name.PrimaryName, name, returnType, parametersMetadata);
                }
            }
            else
            {
                generator.AllocateAssembler();
                parametersMetadata.Generate(generator);
                ParameterMetadata thisParam = parametersMetadata.Find("this");
                if (modifiers.Static)
                {
                    Require.True(thisParam.TypeReference.IsStatic);
                }
                generator.Resolver.SetImplicitFields(thisParam.Slot, thisParam.TypeReference);
                statementMetadata.Prepare(generator);
                generator.Symbols.Source(generator.Assembler.Region.CurrentLocation, this);
                generator.Assembler.StartFunction();
                JumpToken returnToken = generator.Assembler.CreateJumpToken();
                generator.Resolver.RegisterGoto("@return", returnToken);
                JumpToken recurToken = generator.Assembler.CreateJumpToken();
                generator.Assembler.SetDestination(recurToken);
                generator.Resolver.RegisterGoto("@recur", recurToken);
                statementMetadata.Generate(generator, returnType);
                if (modifiers.Abstract)
                {
                    generator.Assembler.Empty();
                    generator.Assembler.CrashIfNull();
                }
                else
                if ((returnType != null) && (!returnType.IsVoid) && (!statementMetadata.Returns()))
                {
                    throw new CompilerException(statementMetadata, string.Format(Resource.Culture, Resource.NotAllCodePathsReturnAValue));
                }
                generator.Assembler.SetDestination(returnToken);
                generator.Assembler.StopFunction();
                generator.Symbols.Source(generator.Assembler.Region.CurrentLocation, this, SourceMark.EndSequence);
                generator.Symbols.WriteCode(generator.Assembler.Region.BaseLocation, generator.Assembler.Region.Length, "method:" + ParentDefinition.Name.Data + "." + name.Data);
                functionPointer = generator.Assembler.Region.BaseLocation;
            }

            generator.Resolver.LeaveContext();
        }