Example #1
0
 public void EmitLoadStaticDataAddress(StaticDataBuilder staticData)
 {
     byte[] code = new byte[5];
     code[0] = (byte)OpCodes.LoadStaticAddress;
     DataHelpers.WriteIntToByteArray(_staticData[staticData], code, 1);
     _code.Add(code);
     _loadStaticDatas[_code.Count - 1] = staticData;
 }
Example #2
0
        public StaticDataBuilder DefineStaticData()
        {
            var staticDataBuilder = new StaticDataBuilder();
            int index             = _staticData.Count;

            _staticData.Add(staticDataBuilder, index);
            return(staticDataBuilder);
        }
Example #3
0
        public Function CreateFunction()
        {
            int position = 0, instructionIndex = 0;
            var finalPositions = new int[_code.Count];

            for (instructionIndex = 0; instructionIndex < _code.Count; ++instructionIndex)
            {
                finalPositions[instructionIndex] = position;
                position += _code[instructionIndex].Length;
            }

            foreach (var pair in _branches)
            {
                int    targetPosition = finalPositions[_labels[pair.Value]];
                byte[] instruction    = _code[pair.Key];
                DataHelpers.WriteIntToByteArray(targetPosition, instruction, 1);
            }

            int[] localOffsets;
            int   offset = 0;

            if (LocalSizes != null)
            {
                localOffsets = LocalSizes.Select(size =>
                {
                    int previousOffset = offset;
                    offset            += size;
                    return(previousOffset);
                }).ToArray();
            }
            else
            {
                localOffsets = new int[0];
            }

            List <Tuple <StaticDataBuilder, List <int> > > staticDataTuples = new List <Tuple <StaticDataBuilder, List <int> > >();

            foreach (var staticDataBuilderPair in _staticData)
            {
                staticDataTuples.Add(new Tuple <StaticDataBuilder, List <int> >(staticDataBuilderPair.Key, new List <int>()));
            }

            var loadStaticDataOffsets = new Dictionary <int, int>();

            foreach (var loadStaticDataPair in _loadStaticDatas)
            {
                int loadStaticDataPosition          = loadStaticDataPair.Key;
                StaticDataBuilder staticDataBuilder = loadStaticDataPair.Value;
                var staticDataTuple = staticDataTuples.First(tuple => tuple.Item1 == staticDataBuilder);
                staticDataTuple.Item2.Add(finalPositions[loadStaticDataPosition]);
            }
            StaticDataInformation[] staticDataInformations = staticDataTuples.Select(
                tuple => new StaticDataInformation(tuple.Item1.Data, tuple.Item2.ToArray(), tuple.Item1.Identifier)
                )
                                                             .ToArray();
            return(new Function(Name, localOffsets, offset, _code.SelectMany(i => i).ToArray(), staticDataInformations));
        }
Example #4
0
        private static void CompileInspect(FunctionCompiler compiler, FunctionalNode inspectNode)
        {
            VariableReference input      = inspectNode.InputTerminals[0].GetTrueVariable();
            int typeSize                 = BytecodeInterpreterAllocator.GetTypeSize(input.Type.GetReferentType());
            StaticDataBuilder staticData = compiler._builder.DefineStaticData();

            staticData.Data       = new byte[typeSize];
            staticData.Identifier = StaticDataIdentifier.CreateFromNode(inspectNode);

            compiler.LoadValueAsReference(input);
            compiler._builder.EmitLoadStaticDataAddress(staticData);
            compiler._builder.EmitLoadIntegerImmediate(typeSize);
            compiler._builder.EmitCopyBytes_TEMP();
        }
Example #5
0
        public bool VisitConstant(Constant constant)
        {
            var output = constant.OutputTerminal.GetTrueVariable();

            if (constant.Value is int)
            {
                LoadLocalAllocationReference(output);
                _builder.EmitLoadIntegerImmediate((int)constant.Value);
                _builder.EmitStoreInteger();
            }
            else if (constant.Value is bool)
            {
                LoadLocalAllocationReference(output);
                _builder.EmitLoadIntegerImmediate((bool)constant.Value ? 1 : 0);
                _builder.EmitStoreInteger();
            }
            else if (constant.Value is string)
            {
                if (output.Type.IsRebarReferenceType() && output.Type.GetReferentType() == DataTypes.StringSliceType)
                {
                    StaticDataBuilder stringStaticData = GetStaticDataForString((string)constant.Value);
                    int stringSize = stringStaticData.Data.Length;

                    LoadStringSliceReferencePointer(output);
                    _builder.EmitLoadStaticDataAddress(stringStaticData);
                    _builder.EmitStorePointer();

                    LoadStringSliceReferenceSizeReference(output);
                    _builder.EmitLoadIntegerImmediate(stringSize);
                    _builder.EmitStoreInteger();
                }
            }
            else
            {
                throw new NotImplementedException();
            }
            return(true);
        }