| 1 |
# crystal comment |
| 2 |
|
| 3 |
require "llvm" |
| 4 |
|
| 5 |
NUM_CELLS = 30000 |
| 6 |
CELL_SIZE_IN_BYTES = 1 |
| 7 |
|
| 8 |
abstract class Instruction |
| 9 |
abstract def compile(program, bb) |
| 10 |
end |
| 11 |
|
| 12 |
class Increment < Instruction |
| 13 |
def initialize(@amount : Int32) |
| 14 |
end |
| 15 |
|
| 16 |
def compile(program, bb) |
| 17 |
cell_val_is_zero = builder.icmp LLVM::IntPredicate::EQ, cell_val, zero |
| 18 |
call_args = [@ctx.int32.const_int(NUM_CELLS), @ctx.int32.const_int(CELL_SIZE_IN_BYTES)] |
| 19 |
builder.cond cell_val_is_zero, loop_after, loop_body_block |
| 20 |
|
| 21 |
@body.each do |instruction| |
| 22 |
loop_body_block = instruction.compile(program, loop_body_block) |
| 23 |
end |
| 24 |
|
| 25 |
builder.position_at_end loop_body_block |
| 26 |
|
| 27 |
unless matching_close_index |
| 28 |
error "Unmatched '[' at position #{i}" |
| 29 |
end |
| 30 |
|
| 31 |
bb |
| 32 |
end |
| 33 |
end |