1 require 'voodoo/generators/common_code_generator'
5 # = ARM GNU Assembler Code Generator
7 # The ARM code generator generates assembly code for use with
10 # == Calling Convention
12 # The first four arguments are passed in the registers r0 through r3.
13 # Any additional arguments are passed on the stack, starting at
14 # r13. r13 will always be a multiple of 8.
16 # The return address for the called function is passed in r14.
18 # The called function will store its return value in r0.
20 # The called function is required to preserve the values of registers
21 # r4 through r11 and register r13.
23 # This calling convention is compatible with the Procedure Call
24 # Standard for the ARM Architecture (AAPCS).
28 # Call frames have the following layout:
30 # When a function is called, it receives a stack frame that looks like
38 # arg4 <-- r13 points here
40 # The function prologue of functions generated by this code generator
41 # creates activation frames that look as follows:
48 # arg4 <-- r11 points here
52 # saved r4 <-- r13 points here
56 # Inside a function, registers r4..r8 and r10 are used for local variables
57 # and function arguments, whereas r11 is used as a frame pointer.
59 # r12 is used as a temporary, and r3 is used when another temporary
62 class ARMGasGenerator < CommonCodeGenerator
65 @WORDSIZE = 1 << @WORDSIZE_BITS
67 @DATA_ALIGNMENT = @WORDSIZE
68 @FUNCTION_ALIGNMENT = @WORDSIZE
69 @STACK_ALIGNMENT_BITS = 3
70 @STACK_ALIGNMENT = 1 << @STACK_ALIGNMENT_BITS
71 # Registers used for storing local variables.
72 @LOCAL_REGISTERS = [:r4, :r5, :r6, :r7, :r8, :r10]
73 # Set of registers used for storing local variables.
74 @LOCAL_REGISTERS_SET = Set.new @LOCAL_REGISTERS
75 # Hash from register names to offsets in saved frame.
76 @SAVED_FRAME_LAYOUT = { :r4 => 0, :r5 => 1, :r6 => 2, :r7 => 3, :r8 => 4,
77 :r9 => 5, :r10 => 6, :r11 => 7, :r13 => 8, :r14 => 9 }
78 @SAVE_FRAME_REGISTERS = @SAVED_FRAME_LAYOUT.keys
79 @INITIAL_FRAME_SIZE = 2 * @WORDSIZE
81 @NREGISTER_LOCALS = @LOCAL_REGISTERS.length
84 @TEMPORARIES = [:r12, :r3, :r2, :r1]
88 @function_end_label = nil
91 @saved_registers = [] # registers we've saved in the current frame
93 @output_file_suffix = '.s'
95 :'bits-per-word' => '32',
96 :'byte-order' => 'little-endian',
97 :'bytes-per-word' => 4
100 # Create an entry in the constants table,
101 # returning the label that will refer to the constant.
102 # The value may be an integer or a label.
103 def add_constant value
105 @constants << [label, value]
109 # Returns the fp-relative offset for the nth (0-based) argument.
111 (n - @NREGISTER_ARGS) * @WORDSIZE
114 # Returns an fp-relative reference for the nth (0-based) argument.
116 offset_reference arg_offset(n)
119 # Return the register in which the nth (0-based) argument is stored, or
120 # nil if not stored in a register
122 if n < @NREGISTER_ARGS
123 # We copied the argument to one of the local registers.
130 def auto_bytes value, register
131 if value.kind_of? Integer
134 temporary = register == :sp ? @TEMPORARIES[0] : register
135 load_value_into_register value, temporary
136 auto_bytes_register temporary
138 emit "cpy #{register}, sp\n" unless register == :sp
141 # auto-bytes where the value is supplied in a register and the return
142 # value will be in sp. register must not be sp.
143 def auto_bytes_register register
144 temporary = register == @TEMPORARIES[0] ? :r3 : @TEMPORARIES[0]
145 emit "add #{register}, #{register}, \##{@STACK_ALIGNMENT - 1}\n"
146 emit "mvn #{temporary}, \##{@STACK_ALIGNMENT - 1}\n"
147 emit "and #{register}, #{register}, #{temporary}\n"
148 emit "sub sp, #{register}\n"
151 def auto_words value, register
152 if value.kind_of? Integer
153 auto_bytes(value * @WORDSIZE, register)
155 raise "Can't use :sp as a register for auto_words" if register == :sp
156 load_value_into_register value, register
157 emit "lsl #{register}, #{register}, \##{@WORDSIZE_BITS}\n"
158 auto_bytes_register register
159 emit "cpy #{register}, sp\n"
163 # Begins a new block.
164 def begin_block *code
165 # If we are starting a block at top level, create a frame
166 if @environment == @top_level
167 nlocals = count_locals code
168 create_frame nlocals, false
170 @environment = Environment.new @environment
173 # Emit function prologue and declare _formals_ as function arguments
174 def begin_function formals, nlocals
175 if @environment != @top_level
176 raise "Can only begin a function at top level"
179 @function_end_label = gensym
180 environment = Environment.new @environment
181 formals.each_with_index do |formal, i|
182 if i < @NREGISTER_ARGS
183 environment.add_arg formal, arg_register(i)
185 environment.add_arg formal, arg_offset(i)
188 @environment = environment
189 emit_function_prologue formals, nlocals
192 # Define a byte with the given value
194 emit ".byte #{value}\n"
199 # Calculate how many arguments need to be pushed on
200 # the stack, and allocate space for them.
201 nstack_args = number_of_stack_arguments args.length
202 old_frame_offset = @frame_offset
203 old_frame_size = @frame_size
204 grow_frame nstack_args if nstack_args > 0
206 # Put stack arguments on the stack
207 (@NREGISTER_ARGS...args.length).each do |n|
208 with_temporary do |temporary|
209 load_value_into_register args[n], temporary
210 emit "str #{temporary}, " +
211 "[sp , \##{(n - @NREGISTER_ARGS) * @WORDSIZE}]\n"
215 # Put register arguments in the right registers
216 nregister_args = number_of_register_arguments args.length
217 nregister_args.times do |n|
218 load_value_into_register args[n], :"r#{n}"
225 with_temporary do |temporary|
226 func_reg = load_value func, temporary
227 emit "blx #{func_reg}\n"
231 # Restore original stack frame
232 if old_frame_size != @frame_size
233 emit "add sp, sp, \##{@frame_size - old_frame_size}\n"
234 @frame_offset = old_frame_offset
235 @frame_size = old_frame_size
239 # Creates a stack frame for the given number of arguments
240 # and local variables.
241 def create_frame nvars, save_lr = true
242 # Calculate how many variables we will store in registers,
243 # and how many on the stack.
244 nregister_vars = [nvars, @NREGISTER_LOCALS].min
245 nstack_vars = nvars - nregister_vars
247 # Save the registers we will clobber to the stack.
249 nregister_vars.times do |i|
250 clobbered << @LOCAL_REGISTERS[i]
253 clobbered << :lr if save_lr
254 @saved_registers = clobbered
255 emit "stmfd sp!, {#{clobbered.join ', '}}\n"
256 emit "add #{@FP}, sp, \##{clobbered.length * @WORDSIZE}\n"
258 # Calculate frame size so that the stack pointer will
259 # be properly aligned at the end of emit_function_prologue.
260 @frame_size = stack_align((clobbered.length + nstack_vars) * @WORDSIZE)
261 extra_space = @frame_size - clobbered.length * @WORDSIZE
263 emit "sub sp, sp, \##{extra_space}\n"
273 # Start a conditional using the specified branch instruction
274 # after the comparison.
275 def common_if comp, x, y = nil
276 with_temporary do |temporary|
277 xreg = load_value x, temporary
278 yreg = load_value y, :a4
280 falselabel = @environment.gensym
281 @if_labels.push falselabel
283 emit "cmp #{xreg}, #{yreg}\n"
284 lut = { :ifeq => "bne", :ifge => "blt", :ifgt => "ble",
285 :ifle => "bgt", :iflt => "bge", :ifne => "beq" }
286 emit "#{lut[comp]} #{falselabel}\n"
290 # Destroys the current stack frame.
291 # If ret is true, loads the saved value of lr into pc.
292 def destroy_frame ret = false
293 # Set sp back to where saved registers were stored
294 saved = @saved_registers
295 emit "sub sp, #{@FP}, \##{saved.length * @WORDSIZE}\n"
298 index = saved.index :lr
302 raise "Request to load saved lr into pc, but lr has not been saved"
305 emit "ldmfd sp!, {#{saved.join ', '}}\n"
307 emit_constants if ret
310 # Aligns on the next multiple of +n+ bytes.
315 # Writes any constants that need to be written to the instruction
316 # stream, and clears the list of constants that need to be written.
318 @constants.each do |x|
325 # Emit function prologue.
326 def emit_function_prologue formals = [], nlocals = 0
327 # Calculate the number of arguments we were passed in
328 # registers, the total number of values we need to save
329 # on the stack, then create a stack frame and save
330 # the registers we will be using.
331 nregister_args = [formals.length, @NREGISTER_ARGS].min
332 nvars = nregister_args + nlocals
333 create_frame nvars, true
335 # Move arguments that were passed in registers into
336 # callee-save registers.
337 nregister_args.times do |i|
338 emit "cpy #{@LOCAL_REGISTERS[i]}, r#{i}\n"
342 # Loads a word into a register.
343 def emit_load_word register, base, offset
345 emit "ldr #{register}, [#{base}]\n"
347 emit "ldr #{register}, [#{base}, \##{offset * @WORDSIZE}]\n"
351 # Stores the value of a register in memory.
352 def emit_store_word register, base, offset
354 emit "str #{register}, [#{base}]\n"
356 emit "str #{register}, [#{base}, \##{offset * @WORDSIZE}]\n"
360 # Ends the current block.
362 # If we are returning to top level, restore stack pointer
363 # and saved registers.
364 if @environment.parent == @top_level
365 offset = @frame_size - @saved_registers.length * @WORDSIZE
367 emit "add sp, sp, \##{offset}\n"
369 emit "ldmfd sp!, {#{@saved_registers.join ', '}}\n"
372 @saved_registers = []
374 # If we need to emit constants, do so now
375 unless @constants.empty?
382 # Restore old value of @environment
383 @environment = @environment.parent
386 # Ends a function body.
388 if @environment == @top_level
389 raise "Cannot end function when not in a function"
392 label @function_end_label
397 @saved_registers = []
398 @environment = @top_level
401 # Ends a conditional.
406 # Evaluate the binary operation expr and store the result in register
407 def eval_binop expr, register
410 # Emulation for div and mod, which ARM does not have instructions for
413 func = :"__aeabi_idiv"
414 import func unless @imports.has_key? func
415 call func, expr[1], expr[2]
416 emit "cpy #{register}, r0\n" if register != :r0
419 func = :"__aeabi_idivmod"
420 import func unless @imports.has_key? func
421 call func, expr[1], expr[2]
422 emit "cpy #{register}, r1\n" if register != :r1
426 with_temporaries(2) do |t1,t2|
427 x = load_value expr[1], t1
431 # Operand must be in a register for these ops.
432 y = load_value expr[2], t2
434 y = value_ref expr[2], t2
439 emit "lsr #{register}, #{x}, #{y}\n"
441 emit "orr #{register}, #{x}, #{y}\n"
443 # Can't store result in same register as first operand.
446 # All using the same register. Move x to a different
447 # register to make this work.
448 temp = (x == t1) ? t2 : t1
449 emit "cpy #{temp}, #{x}\n"
450 emit "mul #{register}, #{temp}, #{y}\n"
452 # Multiplication is commutative. Just swap x and y.
453 emit "mul #{register}, #{y}, #{x}\n"
456 # Common case, register and x are different.
457 emit "mul #{register}, #{x}, #{y}\n"
461 y = "\##{32 - expr[2]}"
463 emit "rsb #{y}, #{y}, #32\n"
465 emit "ror #{register}, #{x}, #{y}\n"
467 emit "lsl #{register}, #{x}, #{y}\n"
469 emit "lsr #{register}, #{x}, #{y}\n"
471 emit "eor #{register}, #{x}, #{y}\n"
473 emit "#{expr[0]} #{register}, #{x}, #{y}\n"
478 # Evaluates the expression +expr+ and stores the result in +register+.
479 def eval_expr expr, register
482 load_value_into_register expr[0], register
484 # Evaluate expression
488 auto_bytes expr[1], register
490 auto_words expr[1], register
493 emit "cpy #{register}, #{@RETURN}\n" if register != @RETURN
495 get_byte expr[1], expr[2], register
497 get_word expr[1], expr[2], register
499 load_value_into_register expr[1], register
500 with_temporary do |temporary|
501 emit "mvn #{temporary}, #0\n"
502 emit "eor #{register}, #{register}, #{temporary}\n"
506 eval_binop expr, register
508 raise "Not a magic word: #{op}"
514 # Export symbols from the current section
516 symbols.each { |sym| emit ".globl #{sym}\n" }
519 # Load byte from _base_ + _offset_ into _register_
520 def get_byte base, offset, register
521 # If base is an integer, but offset isn't, swap them
522 if !integer?(offset) && integer?(base)
523 base, offset = [offset, base]
527 with_temporary do |temporary|
528 base_reg = load_value base, temporary
530 emit "ldrb #{register}, [#{base_reg}]\n"
532 emit "ldrb #{register}, [#{base_reg}, \##{offset}]\n"
536 with_temporaries(2) do |t1,t2|
537 base_reg = load_value base, t1
538 offset_reg = load_value offset, t2
539 emit "ldrb #{register}, [#{base_reg}, #{offset_reg}]\n"
544 # Load word from _base_ + _offset_ * _@WORDSIZE_ into _register_
545 def get_word base, offset, register
547 with_temporary do |temporary|
548 base_reg = load_value base, temporary
550 emit "ldr #{register}, [#{base_reg}]\n"
552 emit "ldr #{register}, [#{base_reg}, \##{offset * @WORDSIZE}]\n"
556 with_temporaries(2) do |t1,t2|
557 base_reg = load_value base, t1
558 offset_reg = load_value offset, t2
559 emit "ldr #{register}, [#{base_reg}, #{offset_reg}, LSL #2]\n"
569 with_temporary do |temporary|
570 register = load_value label, temporary
571 emit "cpy pc, #{register}\n"
575 # If we have constants that need to be emitted, do so now
579 # Grows the current frame by n words, plus padding to
580 # respect alignment rules.
581 def grow_frame nwords
582 increment = stack_align(nwords * @WORDSIZE)
583 emit "sub sp, sp, \##{increment}\n"
584 @frame_size = @frame_size + increment
585 @frame_offset = @frame_offset + increment
588 # Start the false path of a conditional.
590 newlabel = @environment.gensym
594 @if_labels.push newlabel
597 # Test if x is equal to y
599 common_if :ifeq, x, y
602 # Test if x is greater than or equal to y
604 common_if :ifge, x, y
607 # Test if x is strictly greater than y
609 common_if :ifgt, x, y
612 # Test if x is less than or equal to y
614 common_if :ifle, x, y
617 # Test if x is strictly less than y
619 common_if :iflt, x, y
622 # Test if x different from y
624 common_if :ifne, x, y
627 # Import labels into the current section
629 # Record imported labels in @imports
630 symbols.each { |sym| @imports[sym] = sym }
638 # Introduce a new local variable
639 def let symbol, *expr
640 n = @environment.locals
641 register = local_register n
644 # We will use a register to store the value
645 @environment.add_local symbol, register
646 eval_expr expr, register
648 # We will use the stack to store the value
649 offset = local_offset n
650 @environment.add_local symbol, offset
651 with_temporary do |temporary|
652 eval_expr expr, temporary
653 emit "str #{temporary}, #{offset_reference offset}\n"
658 # Loads the value at the given address.
659 def load_at address, register
660 load_value_into_register address, register
661 emit "ldr #{register}, [#{register}]\n"
665 # Loads a value into some register.
666 # If the value is already in a register, does nothing.
667 # Else, loads the value into the register given as the
669 # Returns the name of the register the value is in.
670 def load_value x, register
671 ref = value_ref x, register
675 emit "mov #{register}, #{ref}\n"
680 # Loads a value into a specific register.
681 def load_value_into_register x, register
682 ref = value_ref x, register
685 emit "cpy #{register}, #{ref}\n"
687 emit "mov #{register}, #{ref}\n"
693 # Returns the fp-relative reference for the nth (0-based) local.
695 -@INITIAL_FRAME_SIZE - ((n + number_of_register_arguments) * @WORDSIZE)
698 # Given an offset, returns an fp-relative reference.
699 def offset_reference offset
700 "[#{@FP}, \##{offset}]"
703 # Returns true if the nth (0-based) local is stored in a register
704 def register_local? n
705 (n + number_of_register_arguments) < @NREGISTER_LOCALS
708 # Returns from a function.
710 # _words_ may contain an expression to be evaluated. The result
711 # of the evaluation is returned from the function.
713 # Compute return value and store it in @RETURN
714 eval_expr(words, @RETURN) unless words.empty?
716 goto @function_end_label
719 # Set a variable to the result of evaluating an expression
720 def set symbol, *expr
722 with_temporaries(2) do |t1,t2|
724 register = load_value symbol[1], t2
725 emit "str #{t1}, [#{register}]\n"
728 x = @environment[symbol]
730 raise "Cannot change value of constant #{symbol}"
731 elsif x.kind_of? Symbol
734 with_temporary do |temporary|
735 eval_expr expr, temporary
736 emit "str #{temporary}, #{offset_reference x}\n"
742 # Set the byte at _base_ + _offset_ to _value_
743 def set_byte base, offset, value
744 # If base is an integer, but offset isn't, swap them
745 if !integer?(offset) && integer?(base)
746 base, offset = [offset, base]
750 base_reg = load_value base, :a4
751 with_temporary do |temporary|
752 load_value_into_register value, temporary
754 emit "strb #{temporary}, [#{base_reg}]\n"
756 emit "strb #{temporary}, [#{base_reg}, \##{offset}]\n"
760 eval_binop [:add, base, offset], :a4
761 with_temporary do |temporary|
762 load_value_into_register value, temporary
763 emit "strb #{temporary}, [a4]\n"
768 # Set the word at _base_ + _offset_ * +@WORDSIZE+ to _value_
769 def set_word base, offset, value
770 # If base is an integer, but offset isn't, swap them
771 if !integer?(offset) && integer?(base)
772 base, offset = [offset, base]
776 base_reg = load_value base, :a4
777 with_temporary do |temporary|
778 load_value_into_register value, temporary
780 emit "str #{temporary}, [#{base_reg}]\n"
782 emit "str #{temporary}, [#{base_reg}, \##{offset * @WORDSIZE}]\n"
786 load_value_into_register base, :a4
787 with_temporary do |temporary|
788 load_value_into_register offset, temporary
789 emit "add a4, a4, #{temporary}, LSL #2\n"
790 load_value_into_register value, temporary
791 emit "str #{temporary}, [a4]\n"
796 # Define a string with the given value
799 value.each_byte do |b|
802 elsif b >= 32 && b < 127 && b != 34
805 code << sprintf("\\%03o", b)
808 emit ".ascii \"#{code}\"\n"
811 # Call a function, re-using the current call frame if possible.
812 def tail_call func, *args
813 # Compute number of stack arguments
814 nstackargs = number_of_stack_arguments args.length
815 # If we need more stack arguments than we have now,
816 # perform a normal call and return
817 if nstackargs > number_of_stack_arguments(@environment.args)
818 emit "# Not enough space for proper tail call; using regular call\n"
819 ret :call, func, *args
822 # We will assign arguments from left to right.
823 # Find places that we will overwrite before we read them,
824 # and store their values in some newly allocated stack space.
825 old_frame_offset = @frame_offset
826 old_frame_size = @frame_size
828 (@NREGISTER_ARGS...args.length).each do |i|
830 arg = arg[1] if at_expr? arg
832 binding = @environment[arg]
833 if binding[0] == :arg && binding[1] >= @NREGISTER_ARGS &&
835 # Argument i is a stack argument, but the value we will assign
836 # it is a stack argument that comes before it, so we will
837 # have overwritten it by the time we get to it.
838 overwritten[arg] = nil
843 unless overwritten.empty?
844 # Allocate space for arguments to be saved
845 grow_frame overwritten.length
848 overwritten.each_key do |key|
850 emit "str #{reg}, [sp, \##{offset}]\n"
851 overwritten[key] = offset
852 offset = offset + @WORDSIZE
857 args.each_index do |i|
860 load_value_into_register arg, "a#{i + 1}"
862 # Test if this is a value we saved
863 sym = at_expr?(arg) ? arg[1] : arg
864 saved = overwritten[sym]
866 # Saved value, load from stack
867 with_temporary do |temporary|
868 emit "ldr #{temporary}, [sp, \##{saved}]\n"
869 emit "str #{temporary}, #{arg_reference i}\n"
872 # Regular value, use load_value
873 with_temporary do |temporary|
874 reg = load_value arg, temporary
875 emit "str #{reg}, #{arg_reference i}\n"
881 with_temporary do |temporary|
882 # Load address of function to be called
883 load_value_into_register func, temporary
885 # Destroy current activation frame and enter func
887 emit "bx #{temporary}\n"
892 # Returns a reference to a value.
893 # For immediate values that fit in 8 bits, this returns the
894 # value itself (in ARM syntax).
895 # For all other values, loads the value into a register and
896 # returns the name of the register. If the value is already
897 # in a register, the name of that register is returned. Else,
898 # the value is loaded into the register specified as the
900 def value_ref x, register
902 x = substitute_number x[1]
906 if x >= 0 && x <= 255
908 elsif x >= -255 && x < 0
909 emit "mvn #{register}, \##{-(x + 1)}\n"
913 emit "ldr #{register}, #{lbl}\n"
917 binding = @environment[x]
918 if binding.kind_of? Symbol
919 # Value is already in a register. Return register name.
921 elsif binding.kind_of? Integer
922 # Value is on the stack. Load from the stack.
923 emit "ldr #{register}, #{offset_reference binding}\n"
928 emit "ldr #{register}, #{lbl}\n"
932 load_at x[1], register
934 raise "Don't know how to load #{x.inspect}"
938 # Define a word with the given value
940 emit ".int #{value}\n"
945 # Register class for little endian ARM
946 Voodoo::CodeGenerator.register_generator ARMGasGenerator,
947 :architecture => :arm,