Mercurial > hg > Members > shinya > pyrect
changeset 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 | 5ff3f1efa76a |
children | d40b248a526b |
files | src/benchReg.py src/benchgrep.sh src/cTranslator.py src/converter.py src/grep_bench.sh src/llvm_bench.py src/llvm_translator.py src/reg2llvm.py src/translator.py |
diffstat | 9 files changed, 307 insertions(+), 278 deletions(-) [+] |
line wrap: on
line diff
--- a/src/benchReg.py Mon Jul 05 06:26:20 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -#!/usr/bin/env python - -import sys,re -from optparse import OptionParser -from timeit import Timer -from dfareg import Regexp -from reg2llvm import RegLLVM - -myusage = "%prog [-O] [-p] [-s string] [-r regexp] [-E]" -psr = OptionParser(usage=myusage) -psr.add_option("-O", action="store_true", dest="optimizeFlag", default=False, help="optimimzation") -psr.add_option("-L", action="store_true", dest="implLabel", default=False, help="impliment with label") -psr.add_option("-E", action="store_true", dest="executeFlag", default=False, help="execute Module") -psr.add_option("-p", action="store_true", dest="printFlag", default=False, help="print module") -psr.add_option("-d", action="store_true", dest="dumpFlag", default=False, help="add debug stmt to module") -psr.add_option("-n", action="store", type="int", dest="number", default=1000, help="number of eval", metavar="INT") -psr.add_option("-r", action="store", type="string", dest="regexp", default="(A|B)*C", help="regexp", metavar="FILE") -psr.add_option("-s", action="store", type="string", dest="string", default="A"+"B"*500+"C", help="string", metavar="FILE") -(opts, args) = psr.parse_args(sys.argv) - -print "regexp = \"%s\", string = \"%s\", loop = %d" % (opts.regexp, opts.string, opts.number) - -regLLVM = RegLLVM("regllvm", opts.regexp, opts.string, opts.implLabel, opts.optimizeFlag, opts.dumpFlag) - -if (opts.printFlag): regLLVM.print_module() - -if (opts.executeFlag): - print "LLVM :", Timer(setup='from __main__ import regLLVM', stmt='regLLVM.execute()').timeit(opts.number) - - reg = re.compile("^%s$" % opts.regexp) - print "re :", Timer(setup='from __main__ import reg', stmt='reg.search("%s")' % opts.string).timeit(opts.number) - - reg = Regexp(opts.regexp) - print "dfareg:", Timer(setup='from __main__ import reg', stmt='reg.matches("%s")' % opts.string).timeit(opts.number) -
--- a/src/benchgrep.sh Mon Jul 05 06:26:20 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,22 +0,0 @@ -#!/bin/sh - -egrepout="/tmp/egrep.out" -jitgrepout="/tmp/jitgrep.out" -agrepout="/tmp/agrep.out" - -echo "[jitgrep]" -time ./jitgrep.py $@ > $jitgrepout - -echo "\n[agrep]" -time agrep $@ > $agrepout - -echo "\n[egrep]" -time egrep $@ > $egrepout - -echo "\n[diff egrep jitgrep]" -diff $egrepout $jitgrepout - -echo "[diff agrep jitgrep]" -diff $agrepout $jitgrepout - -#rm -f $egrepout $jitgrepout
--- a/src/cTranslator.py Mon Jul 05 06:26:20 2010 +0900 +++ b/src/cTranslator.py Mon Jul 05 08:36:11 2010 +0900 @@ -5,10 +5,10 @@ class CTranslator(Translator): """CTranslator - This Calss can translate from DFA or NFA into C source code. + This Class can translate from DFA or NFA into C source code. DFA: A simple state transition as tail call (also can implement with CbC). NFA: using stack, deepening depth-first search. - >>> string = \"(A|B)*C\" + >>> string = '(A|B)*C' >>> reg = Regexp(string) >>> dfacg = CallGraph(reg.dfa) >>> nfacg = CallGraph(reg.nfa) @@ -61,11 +61,11 @@ def emit_switch(self, case, default=None): self.emit("\tswitch(s++) {\n") - for input, nextStates in case.iteritems(): + for input, next_states in case.iteritems(): if input != '': self.emit("\t\tcase '%s': \n" % (input)) - for nextState in nextStates: - self.emit("\t\t\t%s%s(s);\n" % (self.callType, self.modify_state_name(nextState))) + for next_state in next_states: + self.emit("\t\t\t%s%s(s);\n" % (self.callType, self.modify_state_name(next_state))) if self.breakStatement != '': self.emit(self.breakStatement+'\n') if default: @@ -114,10 +114,5 @@ def test(): import doctest doctest.testmod() - ''' - reg = Regexp("(A|B)*C") - ct = CTranslator(reg.regexp, CallGraph(reg.dfa)) - ct.translate() - ''' if __name__ == '__main__': test()
--- a/src/converter.py Mon Jul 05 06:26:20 2010 +0900 +++ b/src/converter.py Mon Jul 05 08:36:11 2010 +0900 @@ -4,15 +4,21 @@ from dfareg import Regexp, CallGraph from cTranslator import CTranslator from cbcTranslator import CbCTranslator +from dotTranslator import DotTranslator +from llvm_translator import LLVMTranslator from optparse import OptionParser def main(argv): myusage = "%prog [-C] regexp" psr = OptionParser(usage=myusage) - psr.add_option("-C", action="store_true", dest="emitC", default=False, help="emit C-source") + psr.add_option("--CbC", action="store_true", dest="emit_cbc", default=False, help="emit CbC-source") + psr.add_option("--LLVM", action="store_true", dest="emit_llvm", default=False, help="emit LLVM-source") + psr.add_option("--Dot", action="store_true", dest="emit_dot", default=False, help="emit Dot-source") psr.add_option("--from-dfa", action="store_true", dest="dfa", default=True, help="translate from DFA") psr.add_option("--from-nfa", action="store_true", dest="nfa", default=False, help="translate from NFA") psr.add_option("-o", action="store", type="string", dest="output", default=False, help="output file", metavar="FILE") + psr.add_option("-O", action="store", type="string", dest="optimize", default=False, help="do optimization (only in llvm).", metavar="FILE") + psr.add_option("-L", action="store_true", dest="label", default=False, help="implement with label&goto. (only in llvm).") psr.add_option("-g", action="store_true", dest="debug", default=False, help="embed debug info") psr.add_option("-D", action="store_true", dest="emitDot", default=False, help="emit Dot file") (opts, args) = psr.parse_args(sys.argv) @@ -30,13 +36,19 @@ else: fa = reg.dfa - if opts.emitDot: - r.emitDot() - elif opts.emitC: - translator = CTranslator(reg.regexp, CallGraph(fa)) + if opts.emit_dot: + translator = DotTranslator(reg.regexp, CallGraph(fa)) + translator.debug = opts.debug + elif opts.emit_llvm: + translator = LLVMTranslator(reg.regexp, CallGraph(fa)) + translator.debug = opts.debug + translator.optimize = opts.optimize + translator.impl_label = opts.label + elif opts.emit_cbc: + translator = CbCTranslator(reg.regexp, CallGraph(fa)) translator.debug = opts.debug else: - translator = CbCTranslator(reg.regexp, CallGraph(fa)) + translator = CTranslator(reg.regexp, CallGraph(fa)) translator.debug = opts.debug translator.translate(output)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/grep_bench.sh Mon Jul 05 08:36:11 2010 +0900 @@ -0,0 +1,22 @@ +#!/bin/sh + +egrepout="/tmp/egrep.out" +jitgrepout="/tmp/jitgrep.out" +agrepout="/tmp/agrep.out" + +echo "[jitgrep]" +time ./jitgrep.py $@ > $jitgrepout + +echo "\n[agrep]" +time agrep $@ > $agrepout + +echo "\n[egrep]" +time egrep $@ > $egrepout + +echo "\n[diff egrep jitgrep]" +diff $egrepout $jitgrepout + +echo "[diff agrep jitgrep]" +diff $agrepout $jitgrepout + +#rm -f $egrepout $jitgrepout
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/llvm_bench.py Mon Jul 05 08:36:11 2010 +0900 @@ -0,0 +1,35 @@ +#!/usr/bin/env python + +import sys,re +from optparse import OptionParser +from timeit import Timer +from dfareg import Regexp +from reg2llvm import RegLLVM + +myusage = "%prog [-O] [-p] [-s string] [-r regexp] [-E]" +psr = OptionParser(usage=myusage) +psr.add_option("-O", action="store_true", dest="optimizeFlag", default=False, help="optimimzation") +psr.add_option("-L", action="store_true", dest="implLabel", default=False, help="impliment with label") +psr.add_option("-E", action="store_true", dest="executeFlag", default=False, help="execute Module") +psr.add_option("-p", action="store_true", dest="printFlag", default=False, help="print module") +psr.add_option("-g", action="store_true", dest="dumpFlag", default=False, help="add debug stmt to module") +psr.add_option("-n", action="store", type="int", dest="number", default=1000, help="number of eval", metavar="INT") +psr.add_option("-r", action="store", type="string", dest="regexp", default="(A|B)*C", help="regexp", metavar="FILE") +psr.add_option("-s", action="store", type="string", dest="string", default="A"+"B"*500+"C", help="string", metavar="FILE") +(opts, args) = psr.parse_args(sys.argv) + +print "regexp = \"%s\", string = \"%s\", loop = %d" % (opts.regexp, opts.string, opts.number) + +regLLVM = RegLLVM("regllvm", opts.regexp, opts.string, opts.implLabel, opts.optimizeFlag, opts.dumpFlag) + +if (opts.printFlag): regLLVM.print_module() + +if (opts.executeFlag): + print "LLVM :", Timer(setup='from __main__ import regLLVM', stmt='regLLVM.execute()').timeit(opts.number) + + reg = re.compile("^%s$" % opts.regexp) + print "re :", Timer(setup='from __main__ import reg', stmt='reg.search("%s")' % opts.string).timeit(opts.number) + + reg = Regexp(opts.regexp) + print "dfareg:", Timer(setup='from __main__ import reg', stmt='reg.matches("%s")' % opts.string).timeit(opts.number) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/llvm_translator.py Mon Jul 05 08:36:11 2010 +0900 @@ -0,0 +1,225 @@ +#!/usr/bin/env python + +from llvm.core import * +from llvm.passes import * +from llvm.ee import * +from translator import Translator +from dfareg import Regexp, CallGraph + +class LLVMTranslator(Translator): + """LLVMTranslator + This Class can translate from DFA or NFA into LLVM-IR. + and also can JIT-Compile/evaluate it's self using llvm-py. + >>> string = '(A|B)*C' + >>> reg = Regexp(string) + >>> dfacg = CallGraph(reg.dfa) + >>> lt = LLVMTranslator(string, dfacg) + >>> lt.translate() + >>> isinstance(lt.execute(), llvm.ee.GenericValue) + True + """ + # define llvm core types, and const + int_t = Type.int() + char_t = Type.int(8) + charptr_t = Type.pointer(char_t) + charptrptr_t = Type.pointer(charptr_t) + const_zero = Constant.int(int_t, 0) + const_one = Constant.int(int_t, 1) + llvm.GuaranteedTailCallOpt = True + + def __init__(self, regexp, cg): #(self, modName, regexp, string, self.impl_label, optimized, debug): + Translator.__init__(self, regexp, cg) + self.mod = Module.new(self.cg.type) + self.optimize = False + self.debug = False + self.impl_label = False + self.matchp_str = self.new_str_const("ABC") + self.debug_str = self.new_str_const("state: %s, arg: %c(int %d)\n") + if self.cg.type == "DFA": + self.name_hash = self.create_name_hash() + + def modify_state_name(self, state_name): + if self.cg.type == "DFA": + return self.name_hash[state_name] + else: + return state_name + + def emit_from_callgraph(self): + def optional_func_decl(fun): + fun.calling_convertion = CC_X86_FASTCALL + fun.args[0].name = "index" + + def func_decl(state): + optional_func_decl(state) + + state_ref = dict() + main = self.mod.add_function( + Type.function(self.int_t, (self.int_t,)), "main") + optional_func_decl(main) + main_entry = main.append_basic_block("entry") + + if self.impl_label: + accept_state = main.append_basic_block("accpet") + reject_state = main.append_basic_block("reject") + index_ptr = Builder.new(main_entry).malloc(self.int_t) + Builder.new(accept_state).free(index_ptr) + Builder.new(reject_state).free(index_ptr) + else: + # Create function - accept and reject (final state). + accept_state = self.mod.add_function( + Type.function(self.int_t, (self.int_t,)), "accept") + optional_func_decl(accept_state) + reject_state = self.mod.add_function( + Type.function(self.int_t, (self.int_t,)), "reject") + optional_func_decl(reject_state) + + + state_ref["accept"] = accept_state + state_ref["reject"] = reject_state + + # add state to module, (as function or label). + if (self.impl_label): + for state in self.cg.map.iterkeys(): + label = main.append_basic_block(state) + state_ref[state] = label + else: + for state in self.cg.map.iterkeys(): + fun = self.mod.add_function( + Type.function(self.int_t, (self.int_t,)), state) + optional_func_decl(fun) + state_ref[state] = fun + + # emit instructions + if (self.impl_label): emit = Builder.new(accept_state) + else: emit = Builder.new(accept_state.append_basic_block("entry")) + + self.emit_call_printf(emit, "%s does match regexp\n", self.gep_first(emit, self.matchp_str)) + emit.ret(self.const_one) + + if (self.impl_label): emit = Builder.new(reject_state) + else: emit = Builder.new(reject_state.append_basic_block("entry")) + self.emit_call_printf(emit, "%s does not match regexp\n", self.gep_first(emit, self.matchp_str)) + emit.ret(self.const_zero) + + if (self.impl_label): + # emit transition instruction with jump instruction + emit = Builder.new(main_entry) + emit.store(main.args[0], index_ptr) + emit.branch(state_ref[self.cg.start]) + + for state, transition in self.cg.map.iteritems(): + emit = Builder.new(state_ref[state]) + index = emit.load(index_ptr) + ptr = emit.gep(self.matchp_str, (self.const_zero, index)) + emit.store(emit.add(self.const_one, index), index_ptr) + char = emit.load(ptr) + si = emit.switch(char, state_ref['reject'], len(transition)) + local_label = 0 + for case, next_states in transition.iteritems(): + bb = main.append_basic_block("%s_case%d" % (state, local_label)) #create default bb + emit = Builder.new(bb) + emit.branch(state_ref[next_states[0]]) + si.add_case(self.char_const(case), bb) + local_label += 1 + else: + for state, transition in self.cg.map.iteritems(): + cases = dict() + for case, next_states in transition.iteritems(): + cases[self.char_const(case)] = state_ref[next_states[0]] + state_fun = state_ref[state] + emit = Builder.new(state_fun.append_basic_block("entry")) + ptr = emit.gep(self.matchp_str, (self.const_zero, state_fun.args[0])) + next_index = emit.add(state_fun.args[0], self.const_one) + char = emit.load(ptr) + + if (self.debug): self.emit_call_printf(emit, self.debug_str, self.gep_first(emit, self.new_str_const(fun.name)), char, char) + + label = 0 + default_bb = state_fun.append_basic_block("default") #create default bb + builder = Builder.new(default_bb) # default is reject. + ret = builder.call(reject_state, (next_index,)) + builder.ret(ret) + + si = emit.switch(char, default_bb, len(cases)) # create switch instruction with deafult case. + for case, nextFun in cases.iteritems(): + bb = state_fun.append_basic_block("case%d" % label) #create default bb + builder = Builder.new(bb) + ret = builder.call(nextFun, (next_index,)) + builder.ret(ret) + si.add_case(case, bb) + label += 1 + emit = Builder.new(main_entry) + ret = emit.call(state_ref[self.cg.start], (main.args[0],)) + emit.ret(ret) + + self.mp = ModuleProvider.new(self.mod) + if (self.optimize): self.do_optimize() + self.ee = ExecutionEngine.new(self.mp) + self.main = main + self.emit(str(self.mod)) + + def get_execution_engine(self): + return self.ee + + def do_optimize(self): + #optimization passes + pm = PassManager.new() + pm.add(TargetData.new('')) + pm.add(PASS_FUNCTION_INLINING) + pm.run(self.mod) + fp = FunctionPassManager.new(self.mp) + fp.add(TargetData.new('')) + fp.add(PASS_BLOCK_PLACEMENT) + fp.add(PASS_INSTRUCTION_COMBINING) + fp.add(PASS_TAIL_CALL_ELIMINATION) + fp.add(PASS_AGGRESSIVE_DCE) + fp.add(PASS_DEAD_INST_ELIMINATION) + fp.add(PASS_DEAD_CODE_ELIMINATION) + for fun in self.mod.functions: + fp.run(fun) + + def print_module(self): + print self.mod + + def execute(self): + return self.ee.run_function(self.main, + (GenericValue.int(self.int_t, 0),)) + + def new_str_const(self, val): + '''create string(array of int) as a global value ''' + str = self.mod.add_global_variable(Type.array(self.char_t, len(val) + 1), "") + str.initializer = Constant.stringz(val) + return str + + def gep_first(self, emit, val): + '''get pointer of array''' + return emit.gep(val, (self.const_zero, self.const_zero)) + + def char_const(self, val): + '''create constant int value''' + if isinstance(val, str): + if val == '\\0': + return Constant.int(self.char_t, 0) + else: + return Constant.int(self.char_t, ord(val)) + else: + exit('char_const: invalid argument.', val) + + def emit_call_printf(self, emit, string, *args): + '''emit libc printf function call instruction''' + try: + printf = self.mod.get_function_named("printf") + except llvm.LLVMException: + printf = self.mod.add_function( + Type.function(Type.void(), + (Type.pointer(self.char_t, 0),), 1), "printf") + if isinstance(string, str): + string = self.new_str_const(string) + emit.call(printf, + [self.gep_first(emit, string)]+list(args)) + +def test(): + import doctest + doctest.testmod() + +if __name__ == "__main__": test()
--- a/src/reg2llvm.py Mon Jul 05 06:26:20 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,203 +0,0 @@ -#!/usr/bin/env python - -# Import the llvm-py modules. -# encodingfrom llvm import * -from llvm.core import * -from llvm.passes import * -from llvm.ee import * # new import: ee = Execution Engine -from dfareg import * - -class RegLLVM(object): - # define llvm core types, and const - int_t = Type.int() - char_t = Type.int(8) - charptr_t = Type.pointer(char_t) - charptrptr_t = Type.pointer(charptr_t) - const_zero = Constant.int(int_t, 0) - const_one = Constant.int(int_t, 1) - llvm.GuaranteedTailCallOpt = True - - def __init__(self, modName, regexp, string, impl_label, optimized, dump): - # Create a module - self.mod = Module.new(modName) - self.regexp = regexp - self.optimized = optimized - self.dump = dump - self.impl_label = impl_label - - self.matchp_str = self.new_str_const(string) - self.dump_str = self.new_str_const("state: %s, arg: %c(int %d)\n") - - def optional_func_decl(fun): - fun.calling_convertion = CC_X86_FASTCALL - fun.args[0].name = "index" - - state_list = dict() - main = self.mod.add_function( - Type.function(self.int_t, (self.int_t,)), "main") - optional_func_decl(main) - main_entry = main.append_basic_block("entry") - - - if (impl_label): - accept_state = main.append_basic_block("accpet") - reject_state = main.append_basic_block("reject") - index_ptr = Builder.new(main_entry).malloc(self.int_t) - Builder.new(accept_state).free(index_ptr) - Builder.new(reject_state).free(index_ptr) - else: - # Create function - accept and reject (final state). - accept_state = self.mod.add_function( - Type.function(self.int_t, (self.int_t,)), "accept") - optional_func_decl(accept_state) - reject_state = self.mod.add_function( - Type.function(self.int_t, (self.int_t,)), "reject") - optional_func_decl(reject_state) - - state_list["accept"] = accept_state - state_list["reject"] = reject_state - - # create Regexp Object (Regexp has dfa) - r = Regexp(regexp) - self.cg = CallGraph(r.dfa) - - # add state to module, (as function or label). - if (impl_label): - for state in self.cg.map.iterkeys(): - label = main.append_basic_block(state) - state_list[state] = label - else: - for state in self.cg.map.iterkeys(): - fun = self.mod.add_function( - Type.function(self.int_t, (self.int_t,)), state) - optional_func_decl(fun) - state_list[state] = fun - - # emit instructions - if (impl_label): emit = Builder.new(accept_state) - else: emit = Builder.new(accept_state.append_basic_block("entry")) - - if (dump): self.emit_call_printf(emit, "%s does match regexp\n", self.gep_first(emit, self.matchp_str)) - emit.ret(self.const_one) - - if (impl_label): emit = Builder.new(reject_state) - else: emit = Builder.new(reject_state.append_basic_block("entry")) - if (dump): self.emit_call_printf(emit, "%s does not match regexp\n", self.gep_first(emit, self.matchp_str)) - emit.ret(self.const_zero) - - if (impl_label): - # emit transition instruction with jump instruction - emit = Builder.new(main_entry) - emit.store(main.args[0], index_ptr) - emit.branch(state_list[self.cg.start]) - - for state, transition in self.cg.map.iteritems(): - emit = Builder.new(state_list[state]) - index = emit.load(index_ptr) - ptr = emit.gep(self.matchp_str, (self.const_zero, index)) - emit.store(emit.add(self.const_one, index), index_ptr) - char = emit.load(ptr) - si = emit.switch(char, state_list['reject'], len(transition)) - local_label = 0 - for case, next_state in transition.iteritems(): - bb = main.append_basic_block("%s_case%d" % (state, local_label)) #create default bb - emit = Builder.new(bb) - emit.branch(state_list[next_state]) - si.add_case(self.char_const(case), bb) - local_label += 1 - else: - for state, transition in self.cg.map.iteritems(): - cases = dict() - for case, next_states in transition.iteritems(): - for next_state in next_states: - cases[self.char_const(case)] = state_list[next_state] - state_fun = state_list[state] - emit = Builder.new(state_fun.append_basic_block("entry")) - ptr = emit.gep(self.matchp_str, (self.const_zero, state_fun.args[0])) - next_index = emit.add(state_fun.args[0], self.const_one) - char = emit.load(ptr) - - if (dump): self.emit_call_printf(emit, self.dump_str, self.gep_first(emit, self.new_str_const(fun.name)), char, char) - - label = 0 - default_bb = state_fun.append_basic_block("default") #create default bb - builder = Builder.new(default_bb) # default is reject. - ret = builder.call(reject_state, (next_index,)) - builder.ret(ret) - - si = emit.switch(char, default_bb, len(cases)) # create switch instruction with deafult case. - for case, nextFun in cases.iteritems(): - bb = state_fun.append_basic_block("case%d" % label) #create default bb - builder = Builder.new(bb) - ret = builder.call(nextFun, (next_index,)) - builder.ret(ret) - si.add_case(case, bb) - label += 1 - emit = Builder.new(main_entry) - ret = emit.call(state_list[self.cg.start], (main.args[0],)) - emit.ret(ret) - - self.mp = ModuleProvider.new(self.mod) - if (optimized): self.optimize() - self.ee = ExecutionEngine.new(self.mp) - self.main = main - - def get_execution_engine(self): - return self.ee - - def optimize(self): - #optimization passes - pm = PassManager.new() - pm.add(TargetData.new('')) - pm.add(PASS_FUNCTION_INLINING) - pm.run(self.mod) - fp = FunctionPassManager.new(self.mp) - fp.add(TargetData.new('')) - fp.add(PASS_BLOCK_PLACEMENT) - fp.add(PASS_INSTRUCTION_COMBINING) - fp.add(PASS_TAIL_CALL_ELIMINATION) - fp.add(PASS_AGGRESSIVE_DCE) - fp.add(PASS_DEAD_INST_ELIMINATION) - fp.add(PASS_DEAD_CODE_ELIMINATION) - for fun in self.mod.functions: - fp.run(fun) - - def print_module(self): - print self.mod - - def execute(self): - return self.ee.run_function(self.main, - (GenericValue.int(self.int_t, 0),)) - - def new_str_const(self, val): - '''create string(array of int) as a global value ''' - str = self.mod.add_global_variable(Type.array(self.char_t, len(val) + 1), "") - str.initializer = Constant.stringz(val) - return str - - def gep_first(self, emit, val): - '''get pointer of array''' - return emit.gep(val, (self.const_zero, self.const_zero)) - - def char_const(self, val): - '''create constant int value''' - if isinstance(val, str): - if val == '\\0': - return Constant.int(self.char_t, 0) - else: - return Constant.int(self.char_t, ord(val)) - else: - exit('char_const: invalid argument.', val) - - def emit_call_printf(self, emit, string, *args): - '''emit libc printf function call instruction''' - try: - printf = self.mod.get_function_named("printf") - except llvm.LLVMException: - printf = self.mod.add_function( - Type.function(Type.void(), - (Type.pointer(self.char_t, 0),), 1), "printf") - if isinstance(string, str): - string = self.new_str_const(string) - emit.call(printf, - [self.gep_first(emit, string)]+list(args))
--- a/src/translator.py Mon Jul 05 06:26:20 2010 +0900 +++ b/src/translator.py Mon Jul 05 08:36:11 2010 +0900 @@ -18,8 +18,8 @@ name_hash[states[index]] = str(index) return name_hash - def modify_state_name(self, stateName): - return stateName + def modify_state_name(self, state_name): + return state_name def emit_from_callgraph(self): pass