Mercurial > repos > blastem
comparison cpu_dsl.py @ 1614:c9639139aedf
Did some cleanup of SVP code using the newly more powerful DSL if block and fixed some issues in the DSL implementation that cropped up as a result
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 21 Sep 2018 09:26:12 -0700 |
parents | 2d9e8a7b8ba2 |
children | 28f80d1b343e |
comparison
equal
deleted
inserted
replaced
1613:2d9e8a7b8ba2 | 1614:c9639139aedf |
---|---|
112 | 112 |
113 def generateBody(self, value, prog, otype): | 113 def generateBody(self, value, prog, otype): |
114 output = [] | 114 output = [] |
115 prog.meta = {} | 115 prog.meta = {} |
116 prog.currentScope = self | 116 prog.currentScope = self |
117 self.regValues = {} | |
117 for var in self.locals: | 118 for var in self.locals: |
118 output.append('\n\tuint{sz}_t {name};'.format(sz=self.locals[var], name=var)) | 119 output.append('\n\tuint{sz}_t {name};'.format(sz=self.locals[var], name=var)) |
119 fieldVals,_ = self.getFieldVals(value) | 120 fieldVals,_ = self.getFieldVals(value) |
120 for op in self.implementation: | 121 for op in self.implementation: |
121 op.generate(prog, self, fieldVals, output, otype) | 122 op.generate(prog, self, fieldVals, output, otype) |
508 return self.parent.localSize(name) | 509 return self.parent.localSize(name) |
509 | 510 |
510 def generate(self, prog, parent, fieldVals, output, otype): | 511 def generate(self, prog, parent, fieldVals, output, otype): |
511 oldScope = prog.currentScope | 512 oldScope = prog.currentScope |
512 prog.currentScope = self | 513 prog.currentScope = self |
513 self.regValues = self.parent.regValues | |
514 param = prog.resolveParam(self.param, parent, fieldVals) | 514 param = prog.resolveParam(self.param, parent, fieldVals) |
515 if type(param) is int: | 515 if type(param) is int: |
516 self.regValues = self.parent.regValues | |
516 if param in self.cases: | 517 if param in self.cases: |
517 if self.case_locals[param]: | 518 if self.case_locals[param]: |
518 output.append('\n\t{') | 519 output.append('\n\t{') |
519 for local in self.case_locals[param]: | 520 for local in self.case_locals[param]: |
520 output.append('\n\tuint{0}_t {1};'.format(self.case_locals[param][local], local)) | 521 output.append('\n\tuint{0}_t {1};'.format(self.case_locals[param][local], local)) |
531 op.generate(prog, self, fieldVals, output, otype) | 532 op.generate(prog, self, fieldVals, output, otype) |
532 else: | 533 else: |
533 output.append('\n\tswitch(' + param + ')') | 534 output.append('\n\tswitch(' + param + ')') |
534 output.append('\n\t{') | 535 output.append('\n\t{') |
535 for case in self.cases: | 536 for case in self.cases: |
537 self.regValues = dict(self.parent.regValues) | |
536 output.append('\n\tcase {0}: '.format(case) + '{') | 538 output.append('\n\tcase {0}: '.format(case) + '{') |
537 for local in self.case_locals[case]: | 539 for local in self.case_locals[case]: |
538 output.append('\n\tuint{0}_t {1};'.format(self.case_locals[case][local], local)) | 540 output.append('\n\tuint{0}_t {1};'.format(self.case_locals[case][local], local)) |
539 for op in self.cases[case]: | 541 for op in self.cases[case]: |
540 op.generate(prog, self, fieldVals, output, otype) | 542 op.generate(prog, self, fieldVals, output, otype) |
541 output.append('\n\tbreak;') | 543 output.append('\n\tbreak;') |
542 output.append('\n\t}') | 544 output.append('\n\t}') |
543 if self.default: | 545 if self.default: |
546 self.regValues = dict(self.parent.regValues) | |
544 output.append('\n\tdefault: {') | 547 output.append('\n\tdefault: {') |
545 for local in self.default_locals: | 548 for local in self.default_locals: |
546 output.append('\n\tuint{0}_t {1};'.format(self.default_locals[local], local)) | 549 output.append('\n\tuint{0}_t {1};'.format(self.default_locals[local], local)) |
547 for op in self.default: | 550 for op in self.default: |
548 op.generate(prog, self, fieldVals, output, otype) | 551 op.generate(prog, self, fieldVals, output, otype) |
583 self.cond = cond | 586 self.cond = cond |
584 self.body = [] | 587 self.body = [] |
585 self.elseBody = [] | 588 self.elseBody = [] |
586 self.curBody = self.body | 589 self.curBody = self.body |
587 self.locals = {} | 590 self.locals = {} |
588 self.regValues = parent.regValues | 591 self.elseLocals = {} |
592 self.curLocals = self.locals | |
593 self.regValues = None | |
589 | 594 |
590 def addOp(self, op): | 595 def addOp(self, op): |
591 if op.op in ('case', 'arg'): | 596 if op.op in ('case', 'arg'): |
592 raise Exception(self.op + ' is not allows inside an if block') | 597 raise Exception(self.op + ' is not allows inside an if block') |
593 if op.op == 'local': | 598 if op.op == 'local': |
594 name = op.params[0] | 599 name = op.params[0] |
595 size = op.params[1] | 600 size = op.params[1] |
596 self.locals[name] = size | 601 self.locals[name] = size |
597 elif op.op == 'else': | 602 elif op.op == 'else': |
603 self.curLocals = self.elseLocals | |
598 self.curBody = self.elseBody | 604 self.curBody = self.elseBody |
599 else: | 605 else: |
600 self.curBody.append(op) | 606 self.curBody.append(op) |
601 | 607 |
608 def localSize(self, name): | |
609 return self.curLocals.get(name) | |
610 | |
611 def resolveLocal(self, name): | |
612 if name in self.locals: | |
613 return name | |
614 return None | |
615 | |
616 def _genTrueBody(self): | |
617 self.curLocals = self.locals | |
618 for op in self.body: | |
619 op.generate(prog, self, fieldVals, output, otype) | |
620 | |
621 def _genFalseBody(self): | |
622 self.curLocals = self.elseLocals | |
623 for op in self.body: | |
624 op.generate(prog, self, fieldVals, output, otype) | |
625 | |
626 def _genConstParam(self, param): | |
627 if param: | |
628 self._genTrueBody() | |
629 else: | |
630 self._genFalseBody() | |
631 | |
602 def generate(self, prog, parent, fieldVals, output, otype): | 632 def generate(self, prog, parent, fieldVals, output, otype): |
633 self.regValues = parent.regValues | |
603 try: | 634 try: |
604 if prog.checkBool(self.cond): | 635 self._genConstParam(prog.checkBool(self.cond)) |
605 for op in self.body: | |
606 op.generate(prog, self, fieldVals, output, otype) | |
607 else: | |
608 for op in self.elseBody: | |
609 op.generate(prog, self, fieldVals, output, otype) | |
610 except Exception: | 636 except Exception: |
611 if self.cond in _ifCmpImpl[otype]: | 637 if self.cond in _ifCmpImpl[otype]: |
612 output.append(_ifCmpImpl[otype][self.cond](prog, parent, fieldVals, output)) | 638 output.append(_ifCmpImpl[otype][self.cond](prog, parent, fieldVals, output)) |
613 for op in self.body: | 639 for op in self.body: |
614 op.generate(prog, self, fieldVals, output, otype) | 640 op.generate(prog, self, fieldVals, output, otype) |
625 op.generate(prog, self, fieldVals, output, otype) | 651 op.generate(prog, self, fieldVals, output, otype) |
626 else: | 652 else: |
627 for op in self.elseBody: | 653 for op in self.elseBody: |
628 op.generate(prog, self, fieldVals, output, otype) | 654 op.generate(prog, self, fieldVals, output, otype) |
629 else: | 655 else: |
630 output.append('\n\tif ({cond}) {'.format(cond=cond)) | 656 output.append('\n\tif ({cond}) '.format(cond=cond) + '{') |
631 for op in self.body: | 657 for op in self.body: |
632 op.generate(prog, self, fieldVals, output, otype) | 658 op.generate(prog, self, fieldVals, output, otype) |
633 if self.elseBody: | 659 if self.elseBody: |
634 output.append('\n\t} else {') | 660 output.append('\n\t} else {') |
635 for op in self.elseBody: | 661 for op in self.elseBody: |