Class: Crokus::PrinterC

Inherits:
Object
  • Object
show all
Defined in:
lib/crokus/cfg_printer_c.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PrinterC

Returns a new instance of PrinterC.



7
8
9
10
11
# File 'lib/crokus/cfg_printer_c.rb', line 7

def initialize options={}
  @options=options
  @visited=[]
  @prp=PrettyPrinter.new
end

Instance Attribute Details

#cfgObject

Returns the value of attribute cfg.



5
6
7
# File 'lib/crokus/cfg_printer_c.rb', line 5

def cfg
  @cfg
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/crokus/cfg_printer_c.rb', line 5

def options
  @options
end

Instance Method Details

#decl_arraysObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/crokus/cfg_printer_c.rb', line 100

def decl_arrays
  code=Code.new
  cfg.infos["internal_arrays"].each do |h|
    name,size_lit=h.first
    size_int=size_lit.to_s.to_i
    init=Array.new(size_int){rand(255)}.join(",")
    size=size_lit.accept(@prp)
    code << "int #{name}[#{size}] ={#{init}};"
  end
  code.newline
  code
end

#decl_inputsObject



67
68
69
70
71
# File 'lib/crokus/cfg_printer_c.rb', line 67

def decl_inputs
  if h=cfg.infos["inputs"]
    return h.map{|ident| "int #{ident}"}
  end
end

#decl_loop_indexesObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/crokus/cfg_printer_c.rb', line 88

def decl_loop_indexes
  code=Code.new
  if cfg.infos["loop_indexes"]
    code << "// loop indexes"
    cfg.infos["loop_indexes"].each do |index|
      code << "int #{index};"
    end
    code.newline
  end
  code
end

#decl_outputsObject



73
74
75
76
77
# File 'lib/crokus/cfg_printer_c.rb', line 73

def decl_outputs
  if h=cfg.infos["outputs"]
    return h.map{|ident| "int *#{ident}"}
  end
end

#decl_varsObject



79
80
81
82
83
84
85
86
# File 'lib/crokus/cfg_printer_c.rb', line 79

def decl_vars
  code=Code.new
  if h=cfg.infos["int_vars"]
    h.each{|ident| code << "int #{ident} = #{rand(0..255)};"}
  end
  code.newline
  code
end

#gen_for(bb) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/crokus/cfg_printer_c.rb', line 188

def gen_for bb
  code=Code.new
  @visited << bb
  #code << "// bb 'for' #{bb.label}"
  bb.stmts.each{|stmt| code << stmt.accept(@prp)}
  index=bb.infos["loop_index"]
  index_bound=bb.infos["loop_index_bound"]
  code << "for(#{index}=0;#{index} < #{index_bound};#{index}++){"
  code.indent=2
  code << visit_rec(bb.trueBranch)
  code.indent=0
  code << "}"
  code << visit_rec(bb.falseBranch)
  code
end

#gen_if(bb) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/crokus/cfg_printer_c.rb', line 155

def gen_if bb
  code=Code.new
  @visited << bb
  #code << "// bb 'if' #{bb.label}"
  bb.stmts.each{|stmt| code << stmt.accept(@prp)}
  cond=bb.infos[:cond].accept(@prp)
  code << "if (#{cond}){"
  code.indent=2
  code << visit_rec(bb.trueBranch)
  code.indent=0
  code << "}"
  code << "else {"
  code.indent=2
  code << visit_rec(bb.falseBranch)
  code.indent=0
  code << "}"
  code
end

#gen_plain(bb) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# File 'lib/crokus/cfg_printer_c.rb', line 144

def gen_plain bb
  code=Code.new
  @visited << bb
  #code << "// bb #{bb.label}"
  bb.stmts.each{|assign|
    code << assign.accept(@prp)
  }
  code << visit_rec(bb.nextBranch)
  code
end

#gen_while(bb) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/crokus/cfg_printer_c.rb', line 174

def gen_while bb
  code=Code.new
  @visited << bb
  bb.stmts.each{|stmt| code << stmt.accept(@prp)}
  cond=bb.infos[:cond].accept(@prp)
  code << "while (#{cond}){"
  code.indent=2
  code << visit_rec(bb.trueBranch)
  code.indent=0
  code << "}"
  code << visit_rec(bb.falseBranch)
  code
end

#main(cfg) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/crokus/cfg_printer_c.rb', line 43

def main cfg
  code=Code.new
  code << "int main(void){"
  code.indent=2
  inputs,outputs=[],[]
  cfg.infos["inputs"].each do |input|
    code << "int #{input} = #{rand 0..255};"
    inputs << input
  end
  cfg.infos["outputs"].each do |output|
    code << "int #{output};"
    outputs << "&#{output}"
  end
  params=[inputs,outputs].flatten.join(',')
  code << "#{cfg.name}(#{params});"
  cfg.infos["outputs"].each do |output|
    code << "printf(\"#{output} = %d\\n\",#{output});"
  end
  code << "return 0;"
  code.indent=0
  code << "}"
  code
end

#output_assignsObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/crokus/cfg_printer_c.rb', line 113

def output_assigns
  code=Code.new
  code.newline
  code << "//------- output assignments ------"
  if ary=cfg.infos["output_assigns"]
    ary.each{|h|
      out,expr=h.first
      rhs=expr.accept(@prp)
      if expr.is_a? Parenth
        rhs=expr.expr.accept(@prp)
      end
      code << "*#{out} = #{rhs};"
    }
  end
  code
end


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
# File 'lib/crokus/cfg_printer_c.rb', line 13

def print cfg
  filename= "#{cfg.name}.c"
  puts " |-->[+] generating C code from cfg '#{cfg.name}' in '#{filename}'"
  @cfg=cfg
  code=Code.new
  code << "//"+"-"*60
  code << "// automatically generated by Crokus compiler"
  code << "// date : #{Time.now.strftime("%a %d,%B %Y - %H:%M:%S")}"
  code << "//"+"-"*60
  code.newline
  code << "#include <stdio.h>"
  code << "#include <stdlib.h>"
  code.newline
  io=[decl_inputs,decl_outputs].join(',')
  code << "int #{cfg.name}(#{io}){"
  code.indent=2
  code << decl_vars()
  code << decl_loop_indexes()
  code << decl_arrays()
  code << visit_rec(cfg.starter)
  code << output_assigns()
  code << "return 0;"
  code.indent=0
  code << "}"
  code.newline
  code << main(cfg)
  puts code.finalize if options[:verbose]
  code.save_as filename
end

#visit_rec(bb) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/crokus/cfg_printer_c.rb', line 130

def visit_rec bb
  unless bb.nil? or @visited.include?(bb)
    if bb.infos[:start_if]
      gen_if(bb)
    elsif bb.infos[:start_while]
      gen_while(bb)
    elsif bb.infos[:start_for]
      gen_for(bb)
    else
      gen_plain(bb)
    end
  end
end