#!/bin/env ruby # Step 2 class Compiler def initialize @string_constants = {} @seq = 0 end def get_arg(a) # For now we assume strings only seq = @string_constants[a] return seq if seq seq = @seq @seq += 1 @string_constants[a] = seq return seq end def output_constants puts "\t.section\t.rodata" @string_constants.each do |c,seq| puts ".LC#{seq}:" puts "\t.string \"#{c}\"" end end def compile_exp(exp) call = exp[0].to_s args = exp[1..-1].collect {|a| get_arg(a)} puts "\tsubl\t$4,%esp" args.each do |a| puts "\tmovl\t$.LC#{a},(%esp)" end puts "\tcall\t#{call}" puts "\taddl\t$4, %esp" end def compile(exp) # Taken from gcc -S output puts <