Class: Flux::Assembler
- Inherits:
-
Object
- Object
- Flux::Assembler
- Defined in:
- lib/superinstance/flux-runtime/assembler.rb
Overview
Text assembly to bytecode
Constant Summary collapse
- REGISTER_ALIASES =
{ 'RV' => 8, 'R8' => 8, 'A0' => 9, 'R9' => 9, 'A1' => 10, 'R10' => 10, 'SP' => 11, 'R11' => 11, 'FP' => 12, 'R12' => 12, 'FL' => 13, 'R13' => 13, 'TP' => 14, 'R14' => 14, 'LR' => 15, 'R15' => 15 }.freeze
- OPCODE_MAP =
{ # Control Flow 'HALT' => 0x00, 'NOP' => 0x01, 'RET' => 0x02, 'JUMP' => 0x03, 'JUMPIF' => 0x04, 'JUMPIFNOT' => 0x05, 'CALL' => 0x06, 'CALLINDIRECT' => 0x07, 'YIELD' => 0x08, 'PANIC' => 0x09, 'UNREACHABLE' => 0x0A, # Stack Operations 'PUSH' => 0x10, 'POP' => 0x11, 'DUP' => 0x12, 'SWAP' => 0x13, # Integer Arithmetic 'IMOV' => 0x20, 'IADD' => 0x21, 'ISUB' => 0x22, 'IMUL' => 0x23, 'IDIV' => 0x24, 'IMOD' => 0x25, 'INEG' => 0x26, 'IABS' => 0x27, 'IINC' => 0x28, 'IDEC' => 0x29, 'IMIN' => 0x2A, 'IMAX' => 0x2B, 'IAND' => 0x2C, 'IOR' => 0x2D, 'IXOR' => 0x2E, 'ISHL' => 0x2F, 'ISHR' => 0x30, 'INOT' => 0x31, 'ICMPEQ' => 0x32, 'ICMPNE' => 0x33, 'ICMPLT' => 0x34, 'ICMPLE' => 0x35, 'ICMPGT' => 0x36, 'ICMPGE' => 0x37, # Float Arithmetic 'FMOV' => 0x40, 'FADD' => 0x41, 'FSUB' => 0x42, 'FMUL' => 0x43, 'FDIV' => 0x44, 'FMOD' => 0x45, 'FNEG' => 0x46, 'FABS' => 0x47, 'FSQRT' => 0x48, 'FFLOOR' => 0x49, 'FCEIL' => 0x4A, 'FROUND' => 0x4B, 'FMIN' => 0x4C, 'FMAX' => 0x4D, 'FSIN' => 0x4E, 'FCOS' => 0x4F, 'FEXP' => 0x50, 'FLOG' => 0x51, 'FCLAMP' => 0x52, 'FLERP' => 0x53, 'FCMPEQ' => 0x54, 'FCMPNE' => 0x55, 'FCMPLT' => 0x56, 'FCMPLE' => 0x57, 'FCMPGT' => 0x58, 'FCMPGE' => 0x59, # Conversions 'ITOF' => 0x60, 'FTOI' => 0x61, 'BTOI' => 0x62, 'ITOB' => 0x63, # Memory Operations 'LOAD8' => 0x70, 'LOAD16' => 0x71, 'LOAD32' => 0x72, 'LOAD64' => 0x73, 'STORE8' => 0x74, 'STORE16' => 0x75, 'STORE32' => 0x76, 'STORE64' => 0x77, 'LOADADDR' => 0x78, 'STACKALLOC' => 0x79, # A2A Communication 'ASEND' => 0x80, 'ARECV' => 0x81, 'AASK' => 0x82, 'ATELL' => 0x83, 'ADELEGATE' => 0x84, 'ABROADCAST' => 0x85, 'ASUBSCRIBE' => 0x86, 'AWAIT' => 0x87, 'ATRUST' => 0x88, 'AVERIFY' => 0x89, # Type/Meta 'CAST' => 0x90, 'SIZEOF' => 0x91, 'TYPEOF' => 0x92, # Bitwise 'BAND' => 0xA0, 'BOR' => 0xA1, 'BXOR' => 0xA2, 'BSHL' => 0xA3, 'BSHR' => 0xA4, 'BNOT' => 0xA5, # Vector 'VLOAD' => 0xB0, 'VSTORE' => 0xB1, 'VADD' => 0xB2, 'VMUL' => 0xB3, 'VDOT' => 0xB4 }.freeze
Instance Method Summary collapse
- #assemble(source_code) ⇒ Object
-
#initialize ⇒ Assembler
constructor
A new instance of Assembler.
Constructor Details
#initialize ⇒ Assembler
Returns a new instance of Assembler.
79 80 81 |
# File 'lib/superinstance/flux-runtime/assembler.rb', line 79 def initialize @labels = {} end |
Instance Method Details
#assemble(source_code) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/superinstance/flux-runtime/assembler.rb', line 83 def assemble(source_code) output = [] lines = source_code.lines.map(&:chomp) # Single pass: collect labels and build bytecode @labels = {} pc = 0 # First pass: find labels lines.each do |line| line = strip_comment(line) next if line.empty? if line.end_with?(':') label_name = line.chomp(':').upcase @labels[label_name] = pc else pc += estimate_size(line) end end # Second pass: generate bytecode pc = 0 lines.each do |line| line = strip_comment(line) next if line.empty? next if line.end_with?(':') bytes = encode_instruction(line, pc) output.concat(bytes) pc += bytes.length end output.pack('C*') end |