Class: Generator::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/special-giggle/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, src = nil, dst = nil) ⇒ Token

Returns a new instance of Token.



5
6
7
# File 'lib/special-giggle/generator.rb', line 5

def initialize(type, src = nil, dst = nil)
  @type, @src, @dst = type, src, dst
end

Instance Method Details

#need_imm?Boolean

Returns:

  • (Boolean)


9
10
# File 'lib/special-giggle/generator.rb', line 9

def need_imm?
end

#to_codeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/special-giggle/generator.rb', line 12

def to_code
  case @type
  when :Print
    [ "movq #{@src}, %rsi", "movq $__print_fmt, %rdi", "movq $0, %rax", "call printf" ]
  when :Move
    [ @src.is_a?(Integer) ? "movq $#{@src}, %r11" : "movq #{@src}, %r11", "movq %r11, #{@dst}" ]
  when :Tilde
    [ "movq #{@src}, %r11", "notq %r11", "movq %r11, #{@dst}" ]
  when :Negative
    [ "movq #{@src}, %r11", "negq %r11", "movq %r11, #{@dst}" ]
  when :Plus
    [ "movq #{@src[0]}, %r11", "movq %r11, #{@dst}", "movq #{@src[1]}, %r11", "addq %r11, #{@dst}"]
  when :Minus
    [ "movq #{@src[0]}, %r11", "movq %r11, #{@dst}", "movq #{@src[1]}, %r11", "subq %r11, #{@dst}"]
  when :Asterisk
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "imulq %rcx", "movq %rax, #{@dst}" ]
  when :Slash
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "cqo", "idivq %rcx", "movq %rax, #{@dst}" ]
  when :Percent
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "cqo", "idivq %rcx", "movq %rdx, #{@dst}" ]
  when :Bar
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "orq %rcx, %rax", "movq %rax, #{@dst}" ]
  when :Ampersand
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "andq %rcx, %rax", "movq %rax, #{@dst}" ]
  when :Caret
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "xorq %rcx, %rax", "movq %rax, #{@dst}" ]
  when :LeftShift
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "salq %cl, %rax", "movq %rax, #{@dst}" ]
  when :RightShift
    [ "movq #{@src[0]}, %rax", "movq #{@src[1]}, %rcx", "sarq %cl, %rax", "movq %rax, #{@dst}" ]
  end
end