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; } } }
public void Write(ModuleWriter writer, ChelaModule module) { writer.Write(module.RegisterString(name)); }