Mercurial > hg > Members > shinya > pyrect
annotate src/llvm_translator.py @ 18:ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 05 Jul 2010 08:36:11 +0900 |
parents | |
children | a24acddedf89 |
rev | line source |
---|---|
18
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 from llvm.core import * |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 from llvm.passes import * |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 from llvm.ee import * |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 from translator import Translator |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 from dfareg import Regexp, CallGraph |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 class LLVMTranslator(Translator): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 """LLVMTranslator |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 This Class can translate from DFA or NFA into LLVM-IR. |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 and also can JIT-Compile/evaluate it's self using llvm-py. |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 >>> string = '(A|B)*C' |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 >>> reg = Regexp(string) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 >>> dfacg = CallGraph(reg.dfa) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 >>> lt = LLVMTranslator(string, dfacg) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 >>> lt.translate() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 >>> isinstance(lt.execute(), llvm.ee.GenericValue) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 True |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 """ |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 # define llvm core types, and const |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 int_t = Type.int() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 char_t = Type.int(8) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 charptr_t = Type.pointer(char_t) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 charptrptr_t = Type.pointer(charptr_t) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 const_zero = Constant.int(int_t, 0) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 const_one = Constant.int(int_t, 1) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 llvm.GuaranteedTailCallOpt = True |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 def __init__(self, regexp, cg): #(self, modName, regexp, string, self.impl_label, optimized, debug): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 Translator.__init__(self, regexp, cg) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 self.mod = Module.new(self.cg.type) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 self.optimize = False |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 self.debug = False |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 self.impl_label = False |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 self.matchp_str = self.new_str_const("ABC") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 self.debug_str = self.new_str_const("state: %s, arg: %c(int %d)\n") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 if self.cg.type == "DFA": |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 self.name_hash = self.create_name_hash() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 def modify_state_name(self, state_name): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 if self.cg.type == "DFA": |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 return self.name_hash[state_name] |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 else: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 return state_name |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 def emit_from_callgraph(self): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 def optional_func_decl(fun): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 fun.calling_convertion = CC_X86_FASTCALL |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 fun.args[0].name = "index" |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 def func_decl(state): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 optional_func_decl(state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 state_ref = dict() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 main = self.mod.add_function( |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 Type.function(self.int_t, (self.int_t,)), "main") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 optional_func_decl(main) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 main_entry = main.append_basic_block("entry") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 if self.impl_label: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 accept_state = main.append_basic_block("accpet") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 reject_state = main.append_basic_block("reject") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 index_ptr = Builder.new(main_entry).malloc(self.int_t) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 Builder.new(accept_state).free(index_ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 Builder.new(reject_state).free(index_ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 else: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 # Create function - accept and reject (final state). |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 accept_state = self.mod.add_function( |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 Type.function(self.int_t, (self.int_t,)), "accept") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 optional_func_decl(accept_state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 reject_state = self.mod.add_function( |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 Type.function(self.int_t, (self.int_t,)), "reject") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 optional_func_decl(reject_state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 state_ref["accept"] = accept_state |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 state_ref["reject"] = reject_state |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 # add state to module, (as function or label). |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 if (self.impl_label): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 for state in self.cg.map.iterkeys(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 label = main.append_basic_block(state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 state_ref[state] = label |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 else: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 for state in self.cg.map.iterkeys(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 fun = self.mod.add_function( |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 Type.function(self.int_t, (self.int_t,)), state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 optional_func_decl(fun) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 state_ref[state] = fun |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 # emit instructions |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 if (self.impl_label): emit = Builder.new(accept_state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 else: emit = Builder.new(accept_state.append_basic_block("entry")) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 self.emit_call_printf(emit, "%s does match regexp\n", self.gep_first(emit, self.matchp_str)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 emit.ret(self.const_one) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
98 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 if (self.impl_label): emit = Builder.new(reject_state) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 else: emit = Builder.new(reject_state.append_basic_block("entry")) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101 self.emit_call_printf(emit, "%s does not match regexp\n", self.gep_first(emit, self.matchp_str)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 emit.ret(self.const_zero) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 if (self.impl_label): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105 # emit transition instruction with jump instruction |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 emit = Builder.new(main_entry) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107 emit.store(main.args[0], index_ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 emit.branch(state_ref[self.cg.start]) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 for state, transition in self.cg.map.iteritems(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111 emit = Builder.new(state_ref[state]) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112 index = emit.load(index_ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113 ptr = emit.gep(self.matchp_str, (self.const_zero, index)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 emit.store(emit.add(self.const_one, index), index_ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 char = emit.load(ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 si = emit.switch(char, state_ref['reject'], len(transition)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
117 local_label = 0 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 for case, next_states in transition.iteritems(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 bb = main.append_basic_block("%s_case%d" % (state, local_label)) #create default bb |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 emit = Builder.new(bb) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
121 emit.branch(state_ref[next_states[0]]) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 si.add_case(self.char_const(case), bb) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
123 local_label += 1 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 else: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125 for state, transition in self.cg.map.iteritems(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
126 cases = dict() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
127 for case, next_states in transition.iteritems(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
128 cases[self.char_const(case)] = state_ref[next_states[0]] |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 state_fun = state_ref[state] |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 emit = Builder.new(state_fun.append_basic_block("entry")) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131 ptr = emit.gep(self.matchp_str, (self.const_zero, state_fun.args[0])) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 next_index = emit.add(state_fun.args[0], self.const_one) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 char = emit.load(ptr) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
135 if (self.debug): self.emit_call_printf(emit, self.debug_str, self.gep_first(emit, self.new_str_const(fun.name)), char, char) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
136 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
137 label = 0 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
138 default_bb = state_fun.append_basic_block("default") #create default bb |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
139 builder = Builder.new(default_bb) # default is reject. |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
140 ret = builder.call(reject_state, (next_index,)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 builder.ret(ret) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 si = emit.switch(char, default_bb, len(cases)) # create switch instruction with deafult case. |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
144 for case, nextFun in cases.iteritems(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 bb = state_fun.append_basic_block("case%d" % label) #create default bb |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 builder = Builder.new(bb) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147 ret = builder.call(nextFun, (next_index,)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148 builder.ret(ret) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 si.add_case(case, bb) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150 label += 1 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
151 emit = Builder.new(main_entry) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 ret = emit.call(state_ref[self.cg.start], (main.args[0],)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 emit.ret(ret) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
154 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 self.mp = ModuleProvider.new(self.mod) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156 if (self.optimize): self.do_optimize() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 self.ee = ExecutionEngine.new(self.mp) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 self.main = main |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 self.emit(str(self.mod)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 def get_execution_engine(self): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 return self.ee |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 def do_optimize(self): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 #optimization passes |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 pm = PassManager.new() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167 pm.add(TargetData.new('')) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 pm.add(PASS_FUNCTION_INLINING) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 pm.run(self.mod) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 fp = FunctionPassManager.new(self.mp) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171 fp.add(TargetData.new('')) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 fp.add(PASS_BLOCK_PLACEMENT) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 fp.add(PASS_INSTRUCTION_COMBINING) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 fp.add(PASS_TAIL_CALL_ELIMINATION) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175 fp.add(PASS_AGGRESSIVE_DCE) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 fp.add(PASS_DEAD_INST_ELIMINATION) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 fp.add(PASS_DEAD_CODE_ELIMINATION) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178 for fun in self.mod.functions: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 fp.run(fun) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
180 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181 def print_module(self): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 print self.mod |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184 def execute(self): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185 return self.ee.run_function(self.main, |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 (GenericValue.int(self.int_t, 0),)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 def new_str_const(self, val): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 '''create string(array of int) as a global value ''' |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190 str = self.mod.add_global_variable(Type.array(self.char_t, len(val) + 1), "") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191 str.initializer = Constant.stringz(val) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
192 return str |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194 def gep_first(self, emit, val): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
195 '''get pointer of array''' |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196 return emit.gep(val, (self.const_zero, self.const_zero)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198 def char_const(self, val): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
199 '''create constant int value''' |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 if isinstance(val, str): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201 if val == '\\0': |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 return Constant.int(self.char_t, 0) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203 else: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204 return Constant.int(self.char_t, ord(val)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 else: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206 exit('char_const: invalid argument.', val) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208 def emit_call_printf(self, emit, string, *args): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 '''emit libc printf function call instruction''' |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
210 try: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
211 printf = self.mod.get_function_named("printf") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
212 except llvm.LLVMException: |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
213 printf = self.mod.add_function( |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
214 Type.function(Type.void(), |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215 (Type.pointer(self.char_t, 0),), 1), "printf") |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216 if isinstance(string, str): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 string = self.new_str_const(string) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218 emit.call(printf, |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 [self.gep_first(emit, string)]+list(args)) |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221 def test(): |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
222 import doctest |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 doctest.testmod() |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 |
ec36e784df2e
add LLVMTranslator(Translator) (and remove reg2llvm.py), add --LLVM option to converter.py.
Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225 if __name__ == "__main__": test() |