| 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 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 165 |
| 166 incoming_transitions = {} | 166 incoming_transitions = {} |
| 167 def build_incoming_transitions(state, visitor_state): | 167 def build_incoming_transitions(state, visitor_state): |
| 168 for key, transition_state in state.key_state_iter(): | 168 for key, transition_state in state.key_state_iter(): |
| 169 if not transition_state in incoming_transitions: | 169 if not transition_state in incoming_transitions: |
| 170 incoming_transitions[transition_state] = [] | 170 incoming_transitions[transition_state] = [] |
| 171 incoming_transitions[transition_state].append((key,state)) | 171 incoming_transitions[transition_state].append((key,state)) |
| 172 dfa.visit_all_states(build_incoming_transitions) | 172 dfa.visit_all_states(build_incoming_transitions) |
| 173 | 173 |
| 174 def is_replacement_candidate(state): | 174 def is_replacement_candidate(state): |
| 175 action = state.action() | 175 return (state.action().match_action().name() == 'token' or |
| 176 if not action or not action.match_action(): | 176 state.action().match_action().name() == 'harmony_token') |
| 177 return False | |
| 178 if (action.match_action().name() == 'token' or | |
| 179 action.match_action().name() == 'harmony_token'): | |
| 180 return True | |
| 181 return False | |
| 182 | 177 |
| 183 replacements = {} | 178 replacements = {} |
| 184 for state, incoming in incoming_transitions.items(): | 179 for state, incoming in incoming_transitions.items(): |
| 185 if len(incoming) < 10: | 180 if len(incoming) < 10: |
| 186 continue | 181 continue |
| 187 if not is_replacement_candidate(state): | 182 if not is_replacement_candidate(state): |
| 188 continue | 183 continue |
| 189 # check to see if incoming edges are matched by an outgoing edge | 184 # check to see if incoming edges are matched by an outgoing edge |
| 190 def match_filter((incoming_key, incoming_state)): | 185 def match_filter((incoming_key, incoming_state)): |
| 191 return (incoming_state != state and # drop self transitions | 186 return (incoming_state != state and # drop self transitions |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 counters['goto_start'] += 1 | 229 counters['goto_start'] += 1 |
| 235 else: | 230 else: |
| 236 counters['store_token_and_goto'] += 1 | 231 counters['store_token_and_goto'] += 1 |
| 237 match_action = Term('store_token_and_goto', old_token, state_id) | 232 match_action = Term('store_token_and_goto', old_token, state_id) |
| 238 elif old_action.match_action().name() == 'harmony_token': | 233 elif old_action.match_action().name() == 'harmony_token': |
| 239 new_args = list(old_action.match_action().args()) + [state_id] | 234 new_args = list(old_action.match_action().args()) + [state_id] |
| 240 match_action = Term('store_harmony_token_and_goto', *new_args) | 235 match_action = Term('store_harmony_token_and_goto', *new_args) |
| 241 counters['store_harmony_token_and_goto'] += 1 | 236 counters['store_harmony_token_and_goto'] += 1 |
| 242 else: | 237 else: |
| 243 raise Exception(old_action.match_action()) | 238 raise Exception(old_action.match_action()) |
| 244 return Action(old_action.entry_action(), match_action, | 239 return Action(old_action.entry_action(), |
| 240 match_action, |
| 245 old_action.precedence()) | 241 old_action.precedence()) |
| 246 # map the old state to the new state, with fewer transitions and | 242 # map the old state to the new state, with fewer transitions and |
| 247 # goto actions | 243 # goto actions |
| 248 def replace_state(state, acc): | 244 def replace_state(state, acc): |
| 249 new_state = { | 245 new_state = { |
| 250 'transitions' : {}, | 246 'transitions' : {}, |
| 251 'terminal' : state in self.__dfa.terminal_set(), | 247 'terminal' : state in self.__dfa.terminal_set(), |
| 252 'action' : state.action(), | 248 'action' : state.action(), |
| 253 } | 249 } |
| 254 states[name(state)] = new_state | 250 states[name(state)] = new_state |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 counters['store_harmony_token_and_goto']) | 291 counters['store_harmony_token_and_goto']) |
| 296 print 'transitions removed %s' % counters['removals'] | 292 print 'transitions removed %s' % counters['removals'] |
| 297 print 'do_stored_token actions added %s' % len(store_states) | 293 print 'do_stored_token actions added %s' % len(store_states) |
| 298 self.__dfa = Dfa(self.__dfa.encoding(), start_name, states) | 294 self.__dfa = Dfa(self.__dfa.encoding(), start_name, states) |
| 299 | 295 |
| 300 @staticmethod | 296 @staticmethod |
| 301 def optimize(dfa, log): | 297 def optimize(dfa, log): |
| 302 optimizer = DfaOptimizer(dfa, log) | 298 optimizer = DfaOptimizer(dfa, log) |
| 303 optimizer.replace_tokens_with_gotos() | 299 optimizer.replace_tokens_with_gotos() |
| 304 return optimizer.__dfa | 300 return optimizer.__dfa |
| OLD | NEW |