| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 from dfa import Dfa | 28 from dfa import Dfa |
| 29 | 29 |
| 30 class CodeGenerator: | 30 class CodeGenerator: |
| 31 debug = False |
| 31 | 32 |
| 32 @staticmethod | 33 @staticmethod |
| 33 def key_to_code(key): | 34 def key_to_code(key): |
| 34 code = 'if (' | 35 code = 'if (' |
| 35 first = True | 36 first = True |
| 36 for (kind, r) in key.range_iter(): | 37 for (kind, r) in key.range_iter(): |
| 37 if kind == 'CLASS': # FIXME: add class checks | 38 if kind == 'CLASS': # FIXME: add class checks |
| 38 continue | 39 continue |
| 39 assert kind == 'LATIN_1' | 40 assert kind == 'LATIN_1' |
| 40 if not first: | 41 if not first: |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 | 103 |
| 103 def __dfa_state_to_code(self, state): | 104 def __dfa_state_to_code(self, state): |
| 104 # FIXME: add different check types (if, switch, lookup table) | 105 # FIXME: add different check types (if, switch, lookup table) |
| 105 # FIXME: add action + break / continue | 106 # FIXME: add action + break / continue |
| 106 # FIXME: add default action | 107 # FIXME: add default action |
| 107 code = '' | 108 code = '' |
| 108 if self.__start_node_number == state.node_number(): | 109 if self.__start_node_number == state.node_number(): |
| 109 code += ''' | 110 code += ''' |
| 110 code_start: | 111 code_start: |
| 111 ''' | 112 ''' |
| 113 |
| 112 code += ''' | 114 code += ''' |
| 113 code_%s: | 115 code_%s: |
| 114 //fprintf(stderr, "state %s\\n"); | 116 ''' % state.node_number() |
| 115 ''' % (state.node_number(), | 117 |
| 116 state.node_number()) | 118 if CodeGenerator.debug: |
| 119 code += ''' |
| 120 fprintf(stderr, "state %s\\n"); |
| 121 ''' % state.node_number() |
| 117 | 122 |
| 118 entry_action = state.action().entry_action() if state.action() else None | 123 entry_action = state.action().entry_action() if state.action() else None |
| 119 match_action = state.action().match_action() if state.action() else None | 124 match_action = state.action().match_action() if state.action() else None |
| 120 | 125 |
| 121 if entry_action: | 126 if entry_action: |
| 122 code += self.__action_code_map[entry_action[0]](entry_action[1]) | 127 code += self.__action_code_map[entry_action[0]](entry_action[1]) |
| 123 | 128 |
| 124 code += ''' | 129 if CodeGenerator.debug: |
| 125 //fprintf(stderr, "char at hand is %c (%d)\\n", yych, yych);\n''' | 130 code += ''' |
| 131 fprintf(stderr, "char at hand is %c (%d)\\n", yych, yych); |
| 132 ''' |
| 126 | 133 |
| 127 for key, s in state.transitions().items(): | 134 for key, s in state.transitions().items(): |
| 128 code += CodeGenerator.key_to_code(key) | 135 code += CodeGenerator.key_to_code(key) |
| 129 code += ''' { | 136 code += ''' { |
| 130 FORWARD(); | 137 FORWARD(); |
| 131 goto code_%s; | 138 goto code_%s; |
| 132 } | 139 } |
| 133 ''' % s.node_number() | 140 ''' % s.node_number() |
| 134 | 141 |
| 135 if match_action: | 142 if match_action: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 155 code += self.__dfa_state_to_code(state) | 162 code += self.__dfa_state_to_code(state) |
| 156 return code | 163 return code |
| 157 code = dfa.visit_all_states(f, code) | 164 code = dfa.visit_all_states(f, code) |
| 158 | 165 |
| 159 default_action_code = '' | 166 default_action_code = '' |
| 160 assert(default_action and default_action.match_action()) | 167 assert(default_action and default_action.match_action()) |
| 161 action = default_action.match_action() | 168 action = default_action.match_action() |
| 162 default_action_code = self.__action_code_map[action[0]](action[1]) | 169 default_action_code = self.__action_code_map[action[0]](action[1]) |
| 163 code += ''' | 170 code += ''' |
| 164 CHECK(false); goto code_start; | 171 CHECK(false); goto code_start; |
| 165 default_action: | 172 default_action:''' |
| 166 //fprintf(stderr, "default action\\n"); | 173 if CodeGenerator.debug: |
| 174 code += ''' |
| 175 fprintf(stderr, "default action\\n"); |
| 176 ''' |
| 177 code += ''' |
| 167 %s | 178 %s |
| 168 FORWARD(); | 179 FORWARD(); |
| 169 goto code_%s; | 180 goto code_%s; |
| 170 return 0; | 181 return 0; |
| 171 } | 182 } |
| 172 } | 183 } |
| 173 } | 184 } |
| 174 ''' % (default_action_code, self.__start_node_number) | 185 ''' % (default_action_code, self.__start_node_number) |
| 175 return code | 186 return code |
| 176 | 187 |
| 177 @staticmethod | 188 @staticmethod |
| 178 def rule_processor_to_code(rule_processor, use_mdfa): | 189 def rule_processor_to_code(rule_processor, use_mdfa): |
| 179 if use_mdfa: | 190 if use_mdfa: |
| 180 dfa = rule_processor.default_automata().minimal_dfa() | 191 dfa = rule_processor.default_automata().minimal_dfa() |
| 181 else: | 192 else: |
| 182 dfa = rule_processor.default_automata().dfa() | 193 dfa = rule_processor.default_automata().dfa() |
| 183 return CodeGenerator(dfa, rule_processor.default_action).__process() | 194 return CodeGenerator(dfa, rule_processor.default_action).__process() |
| OLD | NEW |