1 require 'voodoo/generators/common_code_generator'
4 # = ARM GNU Assembler Code Generator
6 # The ARM code generator generates assembly code for use with
9 # == Calling Convention
11 # The first four arguments are passed in the registers r0 through r3.
12 # Any additional arguments are passed on the stack, starting at
13 # r13. r13 will always be a multiple of 8.
15 # The return address for the called function is passed in r14.
17 # The called function will store its return value in r0.
19 # The called function is required to preserve the values of registers
20 # r4 through r11 and register r13.
22 # This calling convention is compatible with the Procedure Call
23 # Standard for the ARM Architecture (AAPCS).
27 # Call frames have the following layout:
29 # When a function is called, it receives a stack frame that looks like
37 # arg4 <-- r13 points here
39 # The function prologue of functions generated by this code generator
40 # creates activation frames that look as follows:
47 # arg4 <-- r11 points here
51 # saved r4 <-- r13 points here
55 # Inside a function, registers r4..r8 and r10 are used for local variables
56 # and function arguments, whereas r11 is used as a frame pointer.
58 # r12 is used as a temporary, and r3 is used when another temporary
61 class ARMGasGenerator < CommonCodeGenerator
64 @WORDSIZE = 1 << @WORDSIZE_BITS
66 @DATA_ALIGNMENT = @WORDSIZE
67 @FUNCTION_ALIGNMENT = @WORDSIZE
68 @STACK_ALIGNMENT_BITS = 3
69 @STACK_ALIGNMENT = 1 << @STACK_ALIGNMENT_BITS
70 @SAVE_FRAME_REGISTERS = [:r4, :r5, :r6, :r7, :r8, :r9,
71 :r10, :r11, :r13, :r14]
72 @INITIAL_FRAME_SIZE = 2 * @WORDSIZE
81 @function_end_label = nil
84 @saved_registers = [] # registers we've saved in the current frame
86 @output_file_suffix = '.s'
88 :'bits-per-word' => '32',
89 :'byte-order' => 'little-endian',
90 :'bytes-per-word' => 4
93 # Create an entry in the constants table,
94 # returning the label that will refer to the constant.
95 # The value may be an integer or a label.
96 def add_constant value
98 @constants << [label, value]
102 def align alignment = nil
104 # Get default alignment
107 alignment = @CODE_ALIGNMENT
109 alignment = @DATA_ALIGNMENT
111 alignment = @FUNCTION_ALIGNMENT
113 # Use data alignment as default
114 alignment = @DATA_ALIGNMENT
117 emit ".align #{alignment}\n" unless alignment == 0
120 # Returns the fp-relative offset for the nth (0-based) argument.
122 (n - @NREGISTER_ARGS) * @WORDSIZE
125 # Returns an fp-relative reference for the nth (0-based) argument.
127 offset_reference arg_offset(n)
130 # Return the register in which the nth (0-based) argument is stored, or
131 # nil if not stored in a register
133 # The first @NREGISTER_ARGS arguments are in the v registers,
134 # the rest are on the stack.
142 def auto_bytes value, register
143 if value.kind_of? Integer
146 temporary = register == :sp ? @TEMPORARY : register
147 load_value_into_register value, temporary
148 auto_bytes_register temporary
150 emit "cpy #{register}, sp\n" unless register == :sp
153 # auto-bytes where the value is supplied in a register and the return
154 # value will be in sp. register must not be sp.
155 def auto_bytes_register register
156 temporary = register == @TEMPORARY ? :r3 : @TEMPORARY
157 emit "add #{register}, #{register}, \##{@STACK_ALIGNMENT - 1}\n"
158 emit "mvn #{temporary}, \##{@STACK_ALIGNMENT - 1}\n"
159 emit "and #{register}, #{register}, #{temporary}\n"
160 emit "sub sp, #{register}\n"
163 def auto_words value, register
164 if value.kind_of? Integer
165 auto_bytes(value * @WORDSIZE, register)
167 temporary = register == :sp ? @TEMPORARY : register
168 load_value_into_register value, temporary
169 emit "lsl #{temporary}, #{temporary}, \##{@WORDSIZE_BITS}\n"
170 auto_bytes_register temporary
171 emit "cpy #{register}, sp\n" unless register == :sp
175 # Begins a new block.
176 def begin_block *code
177 # If we are starting a block at top level, create a frame
178 if @environment == @top_level
179 nlocals = count_locals code
180 create_frame nlocals, false
182 @environment = Environment.new @environment
185 # Emit function prologue and declare _formals_ as function arguments
186 def begin_function formals, nlocals
187 if @environment != @top_level
188 raise "Can only begin a function at top level"
191 @function_end_label = gensym
192 environment = Environment.new @environment
193 formals.each_with_index do |formal, i|
194 if i < @NREGISTER_ARGS
195 environment.add_arg formal, arg_register(i)
197 environment.add_arg formal, arg_offset(i)
200 @environment = environment
201 emit_function_prologue formals, nlocals
204 # Define a byte with the given value
206 emit ".byte #{value}\n"
211 # Calculate how many arguments need to be pushed on
212 # the stack, and allocate space for them.
213 nstack_args = number_of_stack_arguments args.length
214 old_frame_offset = @frame_offset
215 old_frame_size = @frame_size
216 grow_frame nstack_args if nstack_args > 0
218 # Put stack arguments on the stack
219 (@NREGISTER_ARGS...args.length).each do |n|
220 load_value_into_register args[n], @TEMPORARY
221 emit "str #{@TEMPORARY}, " +
222 "[sp , \##{(n - @NREGISTER_ARGS) * @WORDSIZE}]\n"
225 # Put register arguments in the right registers
226 nregister_args = number_of_register_arguments args.length
227 nregister_args.times do |n|
228 load_value_into_register args[n], :"a#{n + 1}"
235 func_reg = load_value func
236 emit "blx #{func_reg}\n"
239 # Restore original stack frame
240 if old_frame_size != @frame_size
241 emit "add sp, sp, \##{@frame_size - old_frame_size}\n"
242 @frame_offset = old_frame_offset
243 @frame_size = old_frame_size
247 # Creates a stack frame for the given number of arguments
248 # and local variables.
249 def create_frame nvars, save_lr = true
250 # Calculate how many variables we will store in registers,
251 # and how many on the stack.
252 nregister_vars = [nvars, @NREGISTER_LOCALS].min
253 nstack_vars = nvars - nregister_vars
255 # Save the registers we will clobber to the stack.
257 nregister_vars.times do |i|
258 clobbered << :"v#{i < 5 ? i + 1 : i + 2}"
261 clobbered << :lr if save_lr
262 @saved_registers = clobbered
263 emit "stmfd sp!, {#{clobbered.join ', '}}\n"
264 emit "add #{@FP}, sp, \##{clobbered.length * @WORDSIZE}\n"
266 # Calculate frame size so that the stack pointer will
267 # be properly aligned at the end of emit_function_prologue.
268 @frame_size = stack_align((clobbered.length + nstack_vars) * @WORDSIZE)
269 extra_space = @frame_size - clobbered.length * @WORDSIZE
271 emit "sub sp, sp, \##{extra_space}\n"
281 # Start a conditional using the specified branch instruction
282 # after the comparison.
283 def common_if comp, x, y = nil
284 emit "# #{comp} #{x} #{y}\n"
286 xreg = load_value x, @TEMPORARY
287 yreg = load_value y, :a4
289 falselabel = @environment.gensym
290 @if_labels.push falselabel
292 emit "cmp #{xreg}, #{yreg}\n"
294 lut = { :ifeq => "bne", :ifge => "blt", :ifgt => "ble",
295 :ifle => "bgt", :iflt => "bge", :ifne => "beq" }
296 emit "#{lut[comp]} #{falselabel}\n"
299 # Destroys the current stack frame.
300 # If ret is true, loads the saved value of lr into pc.
301 def destroy_frame ret = false
302 # Set sp back to where saved registers were stored
303 saved = @saved_registers
304 emit "sub sp, #{@FP}, \##{saved.length * @WORDSIZE}\n"
307 index = saved.index :lr
311 raise "Request to load saved lr into pc, but lr has not been saved"
314 emit "ldmfd sp!, {#{saved.join ', '}}\n"
316 emit_constants if ret
319 # Writes any constants that need to be written to the instruction
320 # stream, and clears the list of constants that need to be written.
322 @constants.each do |x|
329 # Emit function prologue.
330 def emit_function_prologue formals = [], nlocals = 0
331 # Calculate the number of arguments we were passed in
332 # registers, the total number of values we need to save
333 # on the stack, then create a stack frame and save
334 # the v registers we will be using.
335 nregister_args = [formals.length, @NREGISTER_ARGS].min
336 nvars = nregister_args + nlocals
337 create_frame nvars, true
339 # Move arguments that were passed in registers into
340 # callee-save registers.
341 nregister_args.times do |i|
342 emit "cpy v#{i + 1}, a#{i + 1}\n"
346 # Ends the current block.
350 # If we are returning to top level, restore stack pointer
351 # and saved registers.
352 if @environment.parent == @top_level
353 offset = @frame_size - @saved_registers.length * @WORDSIZE
355 emit "add sp, sp, \##{offset}\n"
357 emit "ldmfd sp!, {#{@saved_registers.join ', '}}\n"
360 @saved_registers = []
362 # If we need to emit constants, do so now
363 unless @constants.empty?
370 # Restore old value of @environment
371 @environment = @environment.parent
374 # Ends a function body.
376 if @environment == @top_level
377 raise "Cannot end function when not in a function"
380 emit "# function epilogue\n"
381 label @function_end_label
386 @saved_registers = []
387 emit "# end function\n\n"
388 @environment = @top_level
391 # Ends a conditional.
396 # Evaluate the binary operation expr and store the result in register
397 def eval_binop expr, register
400 # Emulation for div and mod, which ARM does not have instructions for
403 func = :"__aeabi_idiv"
404 import func unless @imports.has_key? func
405 call func, expr[1], expr[2]
406 emit "cpy #{register}, r0\n" if register != :r0
409 func = :"__aeabi_idivmod"
410 import func unless @imports.has_key? func
411 call func, expr[1], expr[2]
412 emit "cpy #{register}, r1\n" if register != :r1
416 x = load_value expr[1], :a4
417 y = load_value expr[2], @TEMPORARY
421 emit "lsr #{register}, #{x}, #{y}\n"
423 emit "orr #{register}, #{x}, #{y}\n"
425 # Can't store result in same register as first operand.
428 # All using the same register. Move x to a different
429 # register to make this work.
430 temp = (x == @TEMPORARY) ? :a4 : @TEMPORARY
431 emit "cpy #{temp}, #{x}\n"
432 emit "mul #{register}, #{temp}, #{y}\n"
434 # Multiplication is commutative. Just swap x and y.
435 emit "mul #{register}, #{y}, #{x}\n"
438 # Common case, register and x are different.
439 emit "mul #{register}, #{x}, #{y}\n"
442 emit "rsb #{y}, #{y}, #32\n"
443 emit "ror #{register}, #{x}, #{y}\n"
445 emit "lsl #{register}, #{x}, #{y}\n"
447 emit "lsr #{register}, #{x}, #{y}\n"
449 emit "eor #{register}, #{x}, #{y}\n"
451 emit "#{expr[0]} #{register}, #{x}, #{y}\n"
455 # Evaluates the expression +expr+ and stores the result in +register+.
456 def eval_expr expr, register
459 load_value_into_register expr[0], register
461 # Evaluate expression
465 auto_bytes expr[1], register
467 auto_words expr[1], register
470 emit "cpy #{register}, #{@RETURN}\n" if register != @RETURN
472 get_byte expr[1], expr[2], register
474 get_word expr[1], expr[2], register
476 load_value_into_register expr[1], register
477 emit "mvn #{@TEMPORARY}, #0\n"
478 emit "eor #{register}, #{register}, #{@TEMPORARY}\n"
481 eval_binop expr, register
483 raise "Not a magic word: #{op}"
489 # Export symbols from the current section
491 symbols.each { |sym| emit ".globl #{sym}\n" }
494 # Load byte from _base_ + _offset_ into _register_
495 def get_byte base, offset, register
496 # If base is an integer, but offset isn't, swap them
497 if !integer?(offset) && integer?(base)
498 base, offset = [offset, base]
502 base_reg = load_value base
504 emit "ldrb #{register}, [#{base_reg}]\n"
506 emit "ldrb #{register}, [#{base_reg}, \##{offset}]\n"
509 base_reg = load_value base
510 offset_reg = load_value offset, :a4
511 emit "ldrb #{register}, [#{base_reg}, #{offset_reg}]\n"
515 # Load word from _base_ + _offset_ * _@WORDSIZE_ into _register_
516 def get_word base, offset, register
518 base_reg = load_value base
520 emit "ldr #{register}, [#{base_reg}]\n"
522 emit "ldr #{register}, [#{base_reg}, \##{offset * @WORDSIZE}]\n"
525 base_reg = load_value base
526 offset_reg = load_value offset, :a4
527 emit "ldr #{register}, [#{base_reg}, #{offset_reg}, LSL #2]\n"
536 register = load_value label
537 emit "cpy pc, #{register}\n"
540 # If we have constants that need to be emitted, do so now
544 # Grows the current frame by n words, plus padding to
545 # respect alignment rules.
546 def grow_frame nwords
547 increment = stack_align(nwords * @WORDSIZE)
548 emit "sub sp, sp, \##{increment}\n"
549 @frame_size = @frame_size + increment
550 @frame_offset = @frame_offset + increment
553 # Start the false path of a conditional.
556 newlabel = @environment.gensym
560 @if_labels.push newlabel
563 # Test if x is equal to y
565 common_if :ifeq, x, y
568 # Test if x is greater than or equal to y
570 common_if :ifge, x, y
573 # Test if x is strictly greater than y
575 common_if :ifgt, x, y
578 # Test if x is less than or equal to y
580 common_if :ifle, x, y
583 # Test if x is strictly less than y
585 common_if :iflt, x, y
588 # Test if x different from y
590 common_if :ifne, x, y
593 # Import labels into the current section
595 # Record imported labels in @imports
596 symbols.each { |sym| @imports[sym] = sym }
604 # Introduce a new local variable
605 def let symbol, *expr
606 n = @environment.locals
607 register = local_register n
610 # We will use a register to store the value
611 @environment.add_local symbol, register
612 eval_expr expr, register
614 # We will use the stack to store the value
615 offset = local_offset n
616 @environment.add_local symbol, offset
617 eval_expr expr, @TEMPORARY
618 emit "str #{@TEMPORARY}, #{offset_reference offset}\n"
622 # Load the value at the given address.
623 def load_at address, register = @TEMPORARY
624 load_value_into_register address, register
625 emit "ldr #{register}, [#{register}]\n"
629 # Load a value into a register.
630 # Returns the name of the register.
631 # If the value was already in a register, the name of that
632 # register is returned.
633 # Else, the value is loaded into a register and the name of
634 # that register is returned. The register to use in that case
635 # may be specified using the optional second argument.
636 def load_value x, register = @TEMPORARY
638 x = substitute_number x[1]
642 if x >= 0 && x <= 255
643 emit "mov #{register}, \##{x}\n"
645 elsif x >= -255 && x < 0
646 emit "mvn #{register}, \##{-(x + 1)}\n"
650 emit "ldr #{register}, #{lbl}\n"
654 binding = @environment[x]
655 if binding.kind_of? String
656 # Value is already in a register. Return register name.
658 elsif binding.kind_of? Integer
659 # Value is on the stack. Load from the stack.
660 emit "ldr #{register}, #{offset_reference binding}\n"
665 emit "ldr #{register}, #{lbl}\n"
669 load_at x[1], register
671 raise "Don't know how to load #{x.inspect}"
675 # Load a value into a specific register
676 def load_value_into_register x, register
677 reg = load_value x, register
679 emit "cpy #{register}, #{reg}\n"
683 # Returns the fp-relative reference for the nth (0-based) local.
685 -@INITIAL_FRAME_SIZE - (n * @WORDSIZE)
688 # Return the register in which the nth local (0-based) is stored, or
689 # nil if not stored in a register
692 n = n + number_of_register_arguments
703 # Calculate the number of register arguments,
704 # given the total number of arguments.
705 def number_of_register_arguments n = @environment.args
706 [n, @NREGISTER_ARGS].min
709 # Calculate the number of stack arguments,
710 # given the total number of arguments.
711 def number_of_stack_arguments n = @environment.args
712 [0, n - @NREGISTER_ARGS].max
715 # Given an offset, returns an fp-relative reference.
716 def offset_reference offset
717 "[#{@FP}, \##{offset}]"
720 # Returns true if the nth (0-based) argument is stored in a register
725 # Returns true if the nth (0-based) local is stored in a register
726 def register_local? n
727 (n + number_of_register_arguments) < @NREGISTER_LOCALS
730 # Restores the frame saved at the given location.
731 def restore_frame location
732 load_value_into_register location, @TEMPORARY
733 @SAVE_FRAME_REGISTERS.each_with_index do |register,i|
734 emit "ldr #{register}, [#{@TEMPORARY}, \##{i * @WORDSIZE}]\n"
738 # Returns from a function.
740 # _words_ may contain an expression to be evaluated. The result
741 # of the evaluation is returned from the function.
743 # Compute return value and store it in @RETURN
744 eval_expr(words, @RETURN) unless words.empty?
746 goto @function_end_label
749 # Saves the current frame to the given location.
750 def save_frame location
751 load_value_into_register location, @TEMPORARY
752 @SAVE_FRAME_REGISTERS.each_with_index do |register,i|
753 emit "str #{register}, [#{@TEMPORARY}, \##{i * @WORDSIZE}]\n"
757 # Returns the number of bytes necessary to save the current frame.
759 @SAVE_FRAME_REGISTERS.length * @WORDSIZE
762 # Set a variable to the result of evaluating an expression
763 def set symbol, *expr
766 register = load_value symbol[1]
767 emit "str r3, [#{register}]\n"
769 x = @environment[symbol]
771 raise "Cannot change value of constant #{symbol}"
772 elsif x.kind_of? String
775 eval_expr expr, @TEMPORARY
776 emit "str #{@TEMPORARY}, #{offset_reference x}\n"
781 # Set the byte at _base_ + _offset_ to _value_
782 def set_byte base, offset, value
783 emit "# set-byte #{base} #{offset} #{value}\n"
784 # If base is an integer, but offset isn't, swap them
785 if !integer?(offset) && integer?(base)
786 base, offset = [offset, base]
790 base_reg = load_value base, :a4
791 load_value_into_register value, @TEMPORARY
793 emit "strb #{@TEMPORARY}, [#{base_reg}]\n"
795 emit "strb #{@TEMPORARY}, [#{base_reg}, \##{offset}]\n"
798 eval_binop [:add, base, offset], :a4
799 load_value_into_register value, @TEMPORARY
800 emit "strb #{@TEMPORARY}, [a4]\n"
804 # Set the word at _base_ + _offset_ * +@WORDSIZE+ to _value_
805 def set_word base, offset, value
806 # If base is an integer, but offset isn't, swap them
807 if !integer?(offset) && integer?(base)
808 base, offset = [offset, base]
812 base_reg = load_value base, :a4
813 load_value_into_register value, @TEMPORARY
815 emit "str #{@TEMPORARY}, [#{base_reg}]\n"
817 emit "str #{@TEMPORARY}, [#{base_reg}, \##{offset * @WORDSIZE}]\n"
820 load_value_into_register base, :a4
821 load_value_into_register offset, @TEMPORARY
822 emit "add a4, a4, #{@TEMPORARY}, LSL #2\n"
823 load_value_into_register value, @TEMPORARY
824 emit "str #{@TEMPORARY}, [a4]\n"
828 # Given n, returns the nearest multiple of @STACK_ALIGNMENT
831 (n + @STACK_ALIGNMENT - 1) / @STACK_ALIGNMENT * @STACK_ALIGNMENT
834 # Define a string with the given value
837 value.each_byte do |b|
840 elsif b >= 32 && b < 127 && b != 34
843 code << sprintf("\\%03o", b)
846 emit ".ascii \"#{code}\"\n"
849 # Call a function, re-using the current call frame if possible.
850 def tail_call func, *args
851 emit "# tail-call #{func} #{args.join ' '}\n"
853 # Compute number of stack arguments
854 nstackargs = number_of_stack_arguments args.length
855 # If we need more stack arguments than we have now,
856 # perform a normal call and return
857 if nstackargs > number_of_stack_arguments(@environment.args)
858 emit "# Not enough space for proper tail call; using regular call\n"
859 ret :call, func, *args
862 # We will assign arguments from left to right.
863 # Find places that we will overwrite before we read them,
864 # and store their values in some newly allocated stack space.
865 old_frame_offset = @frame_offset
866 old_frame_size = @frame_size
868 (@NREGISTER_ARGS...args.length).each do |i|
870 arg = arg[1] if at_expr? arg
872 binding = @environment[arg]
873 if binding[0] == :arg && binding[1] >= @NREGISTER_ARGS &&
875 # Argument i is a stack argument, but the value we will assign
876 # it is a stack argument that comes before it, so we will
877 # have overwritten it by the time we get to it.
878 overwritten[arg] = nil
883 unless overwritten.empty?
884 # Allocate space for arguments to be saved
885 grow_frame overwritten.length
888 overwritten.each_key do |key|
890 emit "str #{reg}, [sp, \##{offset}]\n"
891 overwritten[key] = offset
892 offset = offset + @WORDSIZE
897 args.each_index do |i|
900 load_value_into_register arg, "a#{i + 1}"
902 # Test if this is a value we saved
903 sym = at_expr?(arg) ? arg[1] : arg
904 saved = overwritten[sym]
906 # Saved value, load from stack
908 emit "ldr #{reg}, [sp, \##{saved}]\n"
910 # Regular value, use load_value
913 emit "str #{reg}, #{arg_reference i}\n"
917 # Load address of function to be called into @TEMPORARY
918 load_value_into_register func, @TEMPORARY
920 # Destroy current activation frame and enter func
922 emit "bx #{@TEMPORARY}\n"
926 # Define a word with the given value
928 emit ".int #{value}\n"
933 # Register class for little endian ARM
934 Voodoo::CodeGenerator.register_generator ARMGasGenerator,
935 :architecture => :arm,