Exemplo n.º 1
0
        private int Write(ModuleWriter writer, int parentId, int nextIndex)
        {
            // Write the parent id.
            int myindex = nextIndex;

            writer.Write((sbyte)parentId);

            // Write the number of blocks.
            writer.Write((ushort)blocks.Count);

            // Write the number of catches.
            writer.Write((byte)handlers.Count);

            // Write the cleanup.
            if (cleanup != null)
            {
                writer.Write((int)cleanup.GetIndex());
            }
            else
            {
                writer.Write((int)-1);
            }

            // Write the blocks.
            foreach (BasicBlock block in blocks)
            {
                writer.Write((ushort)block.GetIndex());
            }

            // Write the handlers.
            foreach (Handler handler in handlers)
            {
                writer.Write((uint)module.RegisterType(handler.exception));
                writer.Write((ushort)handler.handler.GetIndex());
            }

            // Write the children.
            foreach (ExceptionContext child in children)
            {
                nextIndex = child.Write(writer, myindex, nextIndex);
            }

            // Return the next index.
            return(nextIndex);
        }
Exemplo n.º 2
0
        public void Write(ChelaModule module, ModuleWriter writer)
        {
            // Write the opcode.
            writer.Write((byte)opcode);

            // Write the instruction arguments.
            InstructionDescription desc = InstructionDescription.GetInstructionTable()[(int)opcode];

            InstructionArgumentType[] args = desc.GetArguments();
            byte n;

            for (int i = 0, k = 0; i < arguments.Length; i++, k++)
            {
                object arg = arguments[i];
                switch (args[k])
                {
                case InstructionArgumentType.UInt8:
                    writer.Write((byte)arg);
                    break;

                case InstructionArgumentType.UInt8V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((byte)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int8:
                    writer.Write((sbyte)arg);
                    break;

                case InstructionArgumentType.Int8V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((sbyte)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.UInt16:
                    writer.Write((ushort)arg);
                    break;

                case InstructionArgumentType.UInt16V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((ushort)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int16:
                    writer.Write((short)arg);
                    break;

                case InstructionArgumentType.Int16V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((short)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.UInt32:
                    writer.Write((uint)arg);
                    break;

                case InstructionArgumentType.UInt32V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((uint)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int32:
                    writer.Write((int)arg);
                    break;

                case InstructionArgumentType.Int32V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((int)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.UInt64:
                    writer.Write((ulong)arg);
                    break;

                case InstructionArgumentType.UInt64V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((ulong)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Int64:
                    writer.Write((long)arg);
                    break;

                case InstructionArgumentType.Int64V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((long)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Fp32:
                    writer.Write((float)arg);
                    break;

                case InstructionArgumentType.Fp32V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((float)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.Fp64:
                    writer.Write((double)arg);
                    break;

                case InstructionArgumentType.Fp64V:
                {
                    n = (byte)arg;
                    writer.Write(n); i++;
                    for (int j = 0; j < n; j++)
                    {
                        writer.Write((double)arguments[i++]);
                    }
                }
                break;

                case InstructionArgumentType.TypeID:
                {
                    uint tyid = module.RegisterType((IChelaType)arg);
                    writer.Write(tyid);
                }
                break;

                case InstructionArgumentType.GlobalID:
                case InstructionArgumentType.FieldID:
                case InstructionArgumentType.FunctionID:
                {
                    ScopeMember member = (ScopeMember)arg;
                    uint        id     = member != null?module.RegisterMember(member) : 0u;

                    writer.Write(id);
                }
                break;

                case InstructionArgumentType.StringID:
                {
                    uint sid = module.RegisterString((string)arg);
                    writer.Write(sid);
                }
                break;

                case InstructionArgumentType.BasicBlockID:
                {
                    BasicBlock bb = (BasicBlock)arg;
                    writer.Write((ushort)bb.GetIndex());
                }
                break;

                case InstructionArgumentType.JumpTable:
                {
                    ushort tableLen = (ushort)arg;
                    writer.Write(tableLen); ++i;
                    for (int j = 0; j < tableLen; j++)
                    {
                        int constant = (int)arguments[i++];
                        writer.Write((int)constant);
                        BasicBlock bb = (BasicBlock)arguments[i++];
                        writer.Write((ushort)bb.GetIndex());
                    }
                }
                break;
                }
            }
        }