code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: o1, o2 = _int_ops(op1, op2) if int(o2) == 0: # A + 0 = 0 + A = A => Do Nothing output = _32bit_oper(o1) output.append('push de') output.append('push hl') return output i...
def _add32(ins)
Pops last 2 bytes from the stack and adds them. Then push the result onto the stack. Optimizations: * If any of the operands is ZERO, then do NOTHING: A + 0 = 0 + A = A
3.146626
3.134705
1.003803
op1, op2 = tuple(ins.quad[2:]) if is_int(op2): if int(op2) == 0: # A - 0 = A => Do Nothing output = _32bit_oper(op1) output.append('push de') output.append('push hl') return output rev = op1[0] != 't' and not is_int(op1) and op2[0] == 't' ...
def _sub32(ins)
Pops last 2 dwords from the stack and subtract them. Then push the result onto the stack. NOTE: The operation is TOP[0] = TOP[-1] - TOP[0] If TOP[0] is 0, nothing is done
5.840409
6.047465
0.965762
op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2): op1, op2 = _int_ops(op1, op2) output = _32bit_oper(op1) if op2 == 1: output.append('push de') output.append('push hl') return output # A * 1 = Nothing if op2 == 0: output...
def _mul32(ins)
Multiplies two last 32bit values on top of the stack and and returns the value on top of the stack Optimizations done: * If any operand is 1, do nothing * If any operand is 0, push 0
5.124603
5.175492
0.990167
op1, op2 = tuple(ins.quad[2:]) rev = op1[0] != 't' and not is_int(op1) and op2[0] == 't' output = _32bit_oper(op1, op2, rev) output.append('call __SUB32') output.append('sbc a, a') output.append('push af') REQUIRES.add('sub32.asm') return output
def _ltu32(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand < 2nd operand (top of the stack). Pushes 0 if False, 1 if True. 32 bit unsigned version
8.516293
8.937057
0.952919
op1, op2 = tuple(ins.quad[2:]) rev = op1[0] != 't' and not is_int(op1) and op2[0] == 't' output = _32bit_oper(op1, op2, rev) output.append('pop bc') output.append('or a') output.append('sbc hl, bc') output.append('ex de, hl') output.append('pop de') output.append('sbc hl, de') ...
def _gtu32(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand > 2nd operand (top of the stack). Pushes 0 if False, 1 if True. 32 bit unsigned version
5.147669
5.491734
0.937349
op1, op2 = tuple(ins.quad[2:]) output = _32bit_oper(op1, op2) output.append('call __EQ32') output.append('push af') REQUIRES.add('eq32.asm') return output
def _eq32(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand == 2nd operand (top of the stack). Pushes 0 if False, 1 if True. 32 bit un/signed version
12.506129
13.569289
0.92165
op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2): op1, op2 = _int_ops(op1, op2) if op2 == 0: # X and False = False if str(op1)[0] == 't': # a temporary term (stack) output = _32bit_oper(op1) # Remove op1 from the stack else: o...
def _and32(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand AND (Logical) 2nd operand (top of the stack). Pushes 0 if False, 1 if True. 32 bit un/signed version
8.937932
9.363237
0.954577
output = _32bit_oper(ins.quad[2]) output.append('call __NOT32') output.append('push af') REQUIRES.add('not32.asm') return output
def _not32(ins)
Negates top (Logical NOT) of the stack (32 bits in DEHL)
19.496674
18.301563
1.065301
output = _32bit_oper(ins.quad[2]) output.append('call __BNOT32') output.append('push de') output.append('push hl') REQUIRES.add('bnot32.asm') return output
def _bnot32(ins)
Negates top (Bitwise NOT) of the stack (32 bits in DEHL)
13.319017
11.749772
1.133555
output = _32bit_oper(ins.quad[2]) output.append('call __NEG32') output.append('push de') output.append('push hl') REQUIRES.add('neg32.asm') return output
def _neg32(ins)
Negates top of the stack (32 bits in DEHL)
15.19089
13.32807
1.139767
output = _32bit_oper(ins.quad[2]) output.append('call __ABS32') output.append('push de') output.append('push hl') REQUIRES.add('abs32.asm') return output
def _abs32(ins)
Absolute value of top of the stack (32 bits in DEHL)
14.085396
13.325296
1.057042
op1, op2 = tuple(ins.quad[2:]) if is_int(op2): output = _32bit_oper(op1) if int(op2) == 0: output.append('push de') output.append('push hl') return output if int(op2) > 1: label = tmp_label() output.append('ld b, %s' % o...
def _shl32(ins)
Logical Left shift 32bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd operand is 0, do nothing
3.005469
3.190614
0.941972
''' Adds 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) if _f_ops(op1, op2) is not None: opa, opb = _f_ops(op1, op2) if opb == 0: # A + 0 => A output = _float_oper(opa) output.extend(_fpush()) return output ...
def _addf(ins)
Adds 2 float values. The result is pushed onto the stack.
6.965955
6.125072
1.137285
''' Divides 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) if is_float(op2) and float(op2) == 1: # Nothing to do. A / 1 = A output = _float_oper(op1) output.extend(_fpush()) return output output = _float_oper(op1, op2) output.ap...
def _divf(ins)
Divides 2 float values. The result is pushed onto the stack.
8.630908
7.313658
1.180108
''' Reminder of div. 2 float values. The result is pushed onto the stack. ''' op1, op2 = tuple(ins.quad[2:]) output = _float_oper(op1, op2) output.append('call __MODF') output.extend(_fpush()) REQUIRES.add('modf.asm') return output
def _modf(ins)
Reminder of div. 2 float values. The result is pushed onto the stack.
19.210651
10.205955
1.882298
''' Compares & pops top 2 operands out of the stack, and checks if the 1st operand < 2nd operand (top of the stack). Pushes 0 if False, 1 if True. Floating Point version ''' op1, op2 = tuple(ins.quad[2:]) output = _float_oper(op1, op2) output.append('call __LTF') output....
def _ltf(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand < 2nd operand (top of the stack). Pushes 0 if False, 1 if True. Floating Point version
13.337155
5.546747
2.404501
''' Negates top of the stack (48 bits) ''' output = _float_oper(ins.quad[2]) output.append('call __NOTF') output.append('push af') REQUIRES.add('notf.asm') return output
def _notf(ins)
Negates top of the stack (48 bits)
26.402483
18.939087
1.394074
''' Changes sign of top of the stack (48 bits) ''' output = _float_oper(ins.quad[2]) output.append('call __NEGF') output.extend(_fpush()) REQUIRES.add('negf.asm') return output
def _negf(ins)
Changes sign of top of the stack (48 bits)
30.956348
20.192635
1.533051
''' Absolute value of top of the stack (48 bits) ''' output = _float_oper(ins.quad[2]) output.append('res 7, e') # Just resets the sign bit! output.extend(_fpush()) return output
def _absf(ins)
Absolute value of top of the stack (48 bits)
30.548748
22.652031
1.34861
result = [] for i in l: if i not in result: result.append(i) return result
def get_uniques(l)
Returns a list with no repeated elements.
2.748488
2.385092
1.152362
r"'.'" # A single char t.value = ord(t.value[1]) t.type = 'INTEGER' return t
def t_CHAR(self, t)
r"'.
8.677275
7.483259
1.159558
r'(%[01]+)|([01]+[bB])' # A Binary integer # Note 00B is a 0 binary, but # 00Bh is a 12 in hex. So this pattern must come # after HEXA if t.value[0] == '%': t.value = t.value[1:] # Remove initial % else: t.value = t.value[:-1] # Remove last 'b'...
def t_BIN(self, t)
r'(%[01]+)|([01]+[bB])
6.613023
5.564745
1.188378
r'[._a-zA-Z][._a-zA-Z0-9]*([ \t]*[:])?' # Any identifier tmp = t.value # Saves original value if tmp[-1] == ':': t.type = 'LABEL' t.value = tmp[:-1].strip() return t t.value = tmp.upper() # Convert it to uppercase, since our internal tables uses u...
def t_INITIAL_ID(self, t)
r'[._a-zA-Z][._a-zA-Z0-9]*([ \t]*[:])?
3.838338
3.360429
1.142217
r'[_a-zA-Z][_a-zA-Z0-9]*' # preprocessor directives t.type = preprocessor.get(t.value.lower(), 'ID') return t
def t_preproc_ID(self, t)
r'[_a-zA-Z][_a-zA-Z0-9]*
4.272569
3.840376
1.112539
r'[[(]' if t.value != '[' and OPTIONS.bracket.value: t.type = 'LPP' return t
def t_LP(self, t)
r'[[(]
24.01075
13.927016
1.724041
r'[])]' if t.value != ']' and OPTIONS.bracket.value: t.type = 'RPP' return t
def t_RP(self, t)
r'[])]
24.714563
14.095568
1.753357
r'\r?\n' t.lexer.lineno += 1 t.lexer.begin('INITIAL') return t
def t_INITIAL_preproc_NEWLINE(self, t)
r'\r?\n
5.305372
3.972871
1.3354
r'\#' if self.find_column(t) == 1: t.lexer.begin('preproc') else: self.t_INITIAL_preproc_error(t)
def t_INITIAL_SHARP(self, t)
r'\#
9.692094
8.354156
1.160152
self.input_data = str self.lex = lex.lex(object=self) self.lex.input(self.input_data)
def input(self, str)
Defines input string, removing current lexer.
4.184246
3.334642
1.254781
self.value = SymbolTYPECAST.make_node(type_, self.value, self.lineno) return self.value is not None
def typecast(self, type_)
Test type casting to the argument expression. On success changes the node value to the new typecast, and returns True. On failure, returns False, and the node value is set to None.
13.037165
9.00621
1.447575
''' Generic array address parameter loading. Emmits output code for setting IX at the right location. bytes = Number of bytes to load: 1 => 8 bit value 2 => 16 bit value / string 4 => 32 bit value / f16 value 5 => 40 bit value ''' output = [] indirect = offset[0]...
def _paddr(offset)
Generic array address parameter loading. Emmits output code for setting IX at the right location. bytes = Number of bytes to load: 1 => 8 bit value 2 => 16 bit value / string 4 => 32 bit value / f16 value 5 => 40 bit value
8.174552
3.818395
2.140834
''' Loads an 8 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value. ''' output = _paddr(ins.quad[2]) output.append('ld a, (hl)') output.append('push af') return output
def _paload8(ins)
Loads an 8 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value.
18.116974
6.062953
2.988144
''' Loads a 16 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value. ''' output = _paddr(ins.quad[2]) output.append('ld e, (hl)') output.append('inc hl') output.append('ld d, (hl)') output.append('ex de, hl') output.append('push h...
def _paload16(ins)
Loads a 16 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value.
10.491938
5.035477
2.083603
''' Load a 32 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value. ''' output = _paddr(ins.quad[2]) output.append('call __ILOAD32') output.append('push de') output.append('push hl') REQUIRES.add('iload32.asm') return output
def _paload32(ins)
Load a 32 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value.
16.756567
7.689537
2.179139
''' Loads a floating point value from a memory address. If 2nd arg. start with '*', it is always treated as an indirect value. ''' output = _paddr(ins.quad[2]) output.append('call __ILOADF') output.extend(_fpush()) REQUIRES.add('iloadf.asm') return output
def _paloadf(ins)
Loads a floating point value from a memory address. If 2nd arg. start with '*', it is always treated as an indirect value.
24.137251
9.625903
2.507531
''' Loads a string value from a memory address. ''' output = _paddr(ins.quad[2]) output.append('call __ILOADSTR') output.append('push hl') REQUIRES.add('loadstr.asm') return output
def _paloadstr(ins)
Loads a string value from a memory address.
17.87672
17.428381
1.025725
''' Stores 2º operand content into address of 1st operand. 1st operand is an array element. Dimensions are pushed into the stack. Use '*' for indirect store on 1st operand (A pointer to an array) ''' output = _paddr(ins.quad[1]) value = ins.quad[2] if value[0] == '*': value = va...
def _pastore8(ins)
Stores 2º operand content into address of 1st operand. 1st operand is an array element. Dimensions are pushed into the stack. Use '*' for indirect store on 1st operand (A pointer to an array)
6.903531
3.259717
2.117831
''' Stores 2º operand content into address of 1st operand. store16 a, x => *(&a) = x Use '*' for indirect store on 1st operand. ''' output = _paddr(ins.quad[1]) value = ins.quad[2] if value[0] == '*': value = value[1:] indirect = True else: indirect = False ...
def _pastore16(ins)
Stores 2º operand content into address of 1st operand. store16 a, x => *(&a) = x Use '*' for indirect store on 1st operand.
8.885287
4.584237
1.938226
''' Stores 2º operand content into address of 1st operand. store16 a, x => *(&a) = x ''' output = _paddr(ins.quad[1]) value = ins.quad[2] if value[0] == '*': value = value[1:] indirect = True else: indirect = False try: value = int(ins.quad[2]) & 0xFFFF...
def _pastore32(ins)
Stores 2º operand content into address of 1st operand. store16 a, x => *(&a) = x
5.650909
4.11157
1.374392
''' Stores 2º operand content into address of 1st operand. storef16 a, x => *(&a) = x ''' output = _paddr(ins.quad[1]) value = ins.quad[2] if value[0] == '*': value = value[1:] indirect = True else: indirect = False try: if indirect: value =...
def _pastoref16(ins)
Stores 2º operand content into address of 1st operand. storef16 a, x => *(&a) = x
6.016507
4.277802
1.406448
''' Stores a floating point value into a memory address. ''' output = _paddr(ins.quad[1]) value = ins.quad[2] if value[0] == '*': value = value[1:] indirect = True else: indirect = False try: if indirect: value = int(value) & 0xFFFF # Inmediate?...
def _pastoref(ins)
Stores a floating point value into a memory address.
6.160501
5.923311
1.040044
''' Stores a string value into a memory address. It copies content of 2nd operand (string), into 1st, reallocating dynamic memory for the 1st str. These instruction DOES ALLOW inmediate strings for the 2nd parameter, starting with '#'. ''' output = _paddr(ins.quad[1]) temporal = False va...
def _pastorestr(ins)
Stores a string value into a memory address. It copies content of 2nd operand (string), into 1st, reallocating dynamic memory for the 1st str. These instruction DOES ALLOW inmediate strings for the 2nd parameter, starting with '#'.
5.356786
3.093563
1.731591
if not isinstance(x, int): # If it is another "thing", just return ZEROs return tuple([0] * bytes) x = x & ((2 << (bytes * 8)) - 1) # mask the initial value result = () for i in range(bytes): result += (x & 0xFF,) x >>= 8 return result
def num2bytes(x, bytes)
Returns x converted to a little-endian t-uple of bytes. E.g. num2bytes(255, 4) = (255, 0, 0, 0)
5.843789
5.308459
1.100845
if self.arg is None or any(x is None for x in self.arg): return None for x in self.arg: if not isinstance(x, int): raise InvalidArgError(self.arg) return self.arg
def argval(self)
Returns the value of the arg (if any) or None. If the arg. is not an integer, an error be triggered.
3.688131
3.014473
1.223474
result = [] op = self.opcode.split(' ') argi = 0 while op: q = op.pop(0) if q == 'XX': for k in range(self.argbytes[argi] - 1): op.pop(0) result.extend(num2bytes(self.argval()[argi], self.argbytes[arg...
def bytes(self)
Returns a t-uple with instruction bytes (integers)
5.654054
5.028136
1.124483
if global_.has_errors > OPTIONS.max_syntax_errors.value: msg = 'Too many errors. Giving up!' msg = "%s:%i: %s" % (global_.FILENAME, lineno, msg) msg_output(msg) if global_.has_errors > OPTIONS.max_syntax_errors.value: sys.exit(1) global_.has_errors += 1
def syntax_error(lineno, msg)
Generic syntax error routine
5.094424
5.25844
0.968809
msg = "%s:%i: warning: %s" % (global_.FILENAME, lineno, msg) msg_output(msg) global_.has_warnings += 1
def warning(lineno, msg)
Generic warning error routine
7.53441
7.390619
1.019456
if OPTIONS.strict.value: syntax_error_undeclared_type(lineno, id_) return if type_ is None: type_ = global_.DEFAULT_TYPE warning(lineno, "Using default implicit type '%s' for '%s'" % (type_, id_))
def warning_implicit_type(lineno, id_, type_=None)
Warning: Using default implicit type 'x'
6.206096
5.212928
1.19052
i = inst.strip(' \t\n').split(' ') I = i[0].lower() # Instruction i = ''.join(i[1:]) op = i.split(',') if I in {'call', 'jp', 'jr'} and len(op) > 1: op = op[1:] + ['f'] elif I == 'djnz': op.append('b') elif I in {'push', 'pop', 'call'}: op.append('sp') # Sp ...
def oper(inst)
Returns operands of an ASM instruction. Even "indirect" operands, like SP if RET or CALL is used.
3.37819
3.26834
1.03361
I = inst(i) if I not in {'call', 'jp', 'jr', 'ret'}: return None # This instruction always execute if I == 'ret': i = [x.lower() for x in i.split(' ') if x != ''] return i[1] if len(i) > 1 else None i = [x.strip() for x in i.split(',')] i = [x.lower() for x in i[0].s...
def condition(i)
Returns the flag this instruction uses or None. E.g. 'c' for Carry, 'nz' for not-zero, etc. That is the condition required for this instruction to execute. For example: ADC A, 0 does NOT have a condition flag (it always execute) whilst RETC does.
3.750346
3.16276
1.185783
result = set() if isinstance(op, str): op = [op] for x in op: if is_8bit_register(x): result = result.union([x]) elif x == 'sp': result.add(x) elif x == 'af': result = result.union(['a', 'f']) elif x == "af'": resu...
def single_registers(op)
Given a list of registers like ['a', 'bc', 'h', 'hl'] returns a set of single registers: ['a', 'b', 'c', 'h', 'l']. Non register parameters, like numbers will be ignored.
3.845639
3.637783
1.057138
ins = inst(i) op = oper(i) if ins in ('or', 'and') and op == ['a']: return ['f'] if ins in {'xor', 'or', 'and', 'neg', 'cpl', 'daa', 'rld', 'rrd', 'rra', 'rla', 'rrca', 'rlca'}: return ['a', 'f'] if ins in {'bit', 'cp', 'scf', 'ccf'}: return ['f'] if ins in {'sub...
def result(i)
Returns which 8-bit registers are used by an asm instruction to return a result.
3.529623
3.507653
1.006264
i += 1 new_block = BasicBlock(block.asm[i:]) block.mem = block.mem[:i] block.asm = block.asm[:i] block.update_labels() new_block.update_labels() new_block.goes_to = block.goes_to block.goes_to = IdentitySet() new_block.label_goes = block.label_goes block.label_goes = [] ...
def block_partition(block, i)
Returns two blocks, as a result of partitioning the given one at i-th instruction.
3.101665
3.025857
1.025053
result = [block] if not block.is_partitionable: return result EDP = END_PROGRAM_LABEL + ':' for i in range(len(block) - 1): if i and block.asm[i] == EDP: # END_PROGRAM label always starts a basic block block, new_block = block_partition(block, i - 1) LABE...
def partition_block(block)
If a block is not partitionable, returns a list with the same block. Otherwise, returns a list with the resulting blocks, recursively.
3.436292
3.38122
1.016288
for cell in MEMORY: if cell.is_label: label = cell.inst LABELS[label] = LabelInfo(label, cell.addr, basic_block)
def get_labels(MEMORY, basic_block)
Traverses memory, to annotate all the labels in the global LABELS table
6.119374
5.833504
1.049005
global MEMORY MEMORY = basic_block.mem get_labels(MEMORY, basic_block) basic_block.mem = MEMORY
def initialize_memory(basic_block)
Initializes global memory array with the given one
7.619335
7.466715
1.02044
i = 0 while i < len(initial_memory): tmp = initial_memory[i] match = RE_LABEL.match(tmp) if not match: i += 1 continue if tmp.rstrip() == match.group(): i += 1 continue initial_memory[i] = tmp[match.end():] in...
def cleanupmem(initial_memory)
Cleans up initial memory. Each label must be ALONE. Each instruction must have an space, etc...
2.923039
2.810408
1.040076
global PROC_COUNTER stack = [[]] hashes = [{}] stackprc = [PROC_COUNTER] used = [{}] # List of hashes of unresolved labels per scope MEMORY = block.mem for cell in MEMORY: if cell.inst.upper() == 'PROC': stack += [[]] hashes += [{}] stackp...
def cleanup_local_labels(block)
Traverses memory, to make any local label a unique global one. At this point there's only a single code block
3.077873
3.012586
1.021671
global BLOCKS global PROC_COUNTER LABELS.clear() JUMP_LABELS.clear() del MEMORY[:] PROC_COUNTER = 0 cleanupmem(initial_memory) if OPTIONS.optimization.value <= 2: return '\n'.join(x for x in initial_memory if not RE_PRAGMA.match(x)) optimize_init() bb = BasicBlock...
def optimize(initial_memory)
This will remove useless instructions
4.980037
4.803319
1.036791
self.regs = {} self.stack = [] self.mem = defaultdict(new_tmp_val) # Dict of label -> value in memory for i in 'abcdefhl': self.regs[i] = new_tmp_val() # Initial unknown state self.regs["%s'" % i] = new_tmp_val() self.regs['ixh'] = new_tmp_val...
def reset(self)
Initial state
2.127698
2.096322
1.014967
self.C = None self.Z = None self.P = None self.S = None
def reset_flags(self)
Resets flags to an "unknown state"
5.710014
5.073197
1.125526
if r is None: return None if r.lower() == '(sp)' and self.stack: return self.stack[-1] if r[:1] == '(': return self.mem[r[1:-1]] r = r.lower() if is_number(r): return str(valnum(r)) if not is_register(r): ...
def get(self, r)
Returns precomputed value of the given expression
5.170874
5.14268
1.005482
v = self.get(r) if not is_unknown(v): try: v = int(v) except ValueError: v = None else: v = None return v
def getv(self, r)
Like the above, but returns the <int> value.
3.519539
2.939759
1.19722
if not is_register(r1) or not is_register(r2): return False if self.regs[r1] is None or self.regs[r2] is None: # HINT: This's been never USED?? return False return self.regs[r1] == self.regs[r2]
def eq(self, r1, r2)
True if values of r1 and r2 registers are equal
4.862788
4.553177
1.067999
self.set_flag(None) if not is_register(r): if r[0] == '(': # a memory position, basically: inc(hl) r_ = r[1:-1].strip() v_ = self.getv(self.mem.get(r_, None)) if v_ is not None: v_ = (v_ + 1) & 0xFF ...
def inc(self, r)
Does inc on the register and precomputes flags
4.939744
4.717633
1.047081
if not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return v_ = self.getv(self.regs[r]) & 0xFF self.regs[r] = str((v_ >> 1) | ((v_ & 1) << 7))
def rrc(self, r)
Does a ROTATION to the RIGHT |>>
5.501492
5.276896
1.042562
if self.C is None or not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return self.rrc(r) tmp = self.C v_ = self.getv(self.regs[r]) self.C = v_ >> 7 self.regs[r] = str((v_ & 0x7F) | (tmp << 7))
def rr(self, r)
Like the above, bus uses carry
5.50003
5.268851
1.043877
if not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return v_ = self.getv(self.regs[r]) & 0xFF self.set(r, ((v_ << 1) & 0xFF) | (v_ >> 7))
def rlc(self, r)
Does a ROTATION to the LEFT <<|
5.141462
4.941193
1.040531
if self.C is None or not is_number(self.regs[r]): self.set(r, None) self.set_flag(None) return self.rlc(r) tmp = self.C v_ = self.getv(self.regs[r]) self.C = v_ & 1 self.regs[r] = str((v_ & 0xFE) | tmp)
def rl(self, r)
Like the above, bus uses carry
6.182892
5.911375
1.045931
if not is_register(r) or val is None: return False r = r.lower() if is_register(val): return self.eq(r, val) if is_number(val): val = str(valnum(val)) else: val = str(val) if val[0] == '(': val = self...
def _is(self, r, val)
True if value of r is val.
4.10555
4.081696
1.005844
i = [x for x in self.asm.strip(' \t\n').split(' ') if x != ''] if len(i) == 1: return [] i = ''.join(i[1:]).split(',') if self.condition_flag is not None: i = i[1:] else: i = i[0:] op = [x.lower() if is_register(x) else x fo...
def opers(self)
Returns a list of operators this mnemonic uses
4.820405
4.190307
1.15037
if self.asm in arch.zx48k.backend.ASMS: return ALL_REGS res = set([]) i = self.inst o = self.opers if i in {'push', 'ret', 'call', 'rst', 'reti', 'retn'}: return ['sp'] if i == 'pop': res.update('sp', single_registers(o[:1]...
def destroys(self)
Returns which single registers (including f, flag) this instruction changes. Registers are: a, b, c, d, e, i, h, l, ixh, ixl, iyh, iyl, r LD a, X => Destroys a LD a, a => Destroys nothing INC a => Destroys a, f POP af => Destroys a, f, sp PUSH af => Destroys sp...
3.34712
3.263764
1.02554
if isinstance(reglist, str): reglist = [reglist] reglist = single_registers(reglist) return len([x for x in self.destroys if x in reglist]) > 0
def affects(self, reglist)
Returns if this instruction affects any of the registers in reglist.
4.936899
4.130776
1.19515
if isinstance(reglist, str): reglist = [reglist] reglist = single_registers(reglist) return len([x for x in self.requires if x in reglist]) > 0
def needs(self, reglist)
Returns if this instruction need any of the registers in reglist.
4.407534
3.862385
1.141143
result = [] tmp = self.asm.strip(' \n\r\t') if not len(tmp) or tmp[0] in ('#', ';'): return result try: tmpLexer = asmlex.lex.lex(object=asmlex.Lexer(), lextab='zxbasmlextab') tmpLexer.input(tmp) while True: toke...
def used_labels(self)
Returns a list of required labels for this instruction
5.30063
4.746476
1.116751
if oldLabel == newLabel: return tmp = re.compile(r'\b' + oldLabel + r'\b') last = 0 l = len(newLabel) while True: match = tmp.search(self.asm[last:]) if not match: break txt = self.asm self.asm...
def replace_label(self, oldLabel, newLabel)
Replaces old label with a new one
2.97405
2.945366
1.009739
if len(self.mem) < 2: return False # An atomic block if any(x.is_ender or x.asm in arch.zx48k.backend.ASMS for x in self.mem): return True for label in JUMP_LABELS: if LABELS[label].basic_block == self and (not self.mem[0].is_label or self.mem[0].i...
def is_partitionable(self)
Returns if this block can be partitiones in 2 or more blocks, because if contains enders.
11.115698
10.073615
1.103447
if basic_block is None: return if self.lock: return self.lock = True if self.prev is basic_block: if self.prev.next is self: self.prev.next = None self.prev = None for i in range(len(self.comes_from)): ...
def delete_from(self, basic_block)
Removes the basic_block ptr from the list for "comes_from" if it exists. It also sets self.prev to None if it is basic_block.
2.64848
2.157217
1.22773
if basic_block is None: return if self.lock: return # Return if already added if basic_block in self.comes_from: return self.lock = True self.comes_from.add(basic_block) basic_block.add_goes_to(self) self.loc...
def add_comes_from(self, basic_block)
This simulates a set. Adds the basic_block to the comes_from list if not done already.
2.79692
2.689686
1.039869
last = self.mem[-1] if last.inst not in ('ret', 'jp', 'jr') or last.condition_flag is not None: return if last.inst == 'ret': if self.next is not None: self.next.delete_from(self) self.delete_goes(self.next) return ...
def update_next_block(self)
If the last instruction of this block is a JP, JR or RET (with no conditions) then the next and goes_to sets just contains a single block
4.963905
4.592459
1.080882
# Remove any block from the comes_from and goes_to list except the PREVIOUS and NEXT if not len(self): return if self.mem[-1].inst == 'ret': return # subroutine returns are updated from CALLer blocks self.update_used_by_list() if not self.mem[...
def update_goes_and_comes(self)
Once the block is a Basic one, check the last instruction and updates goes_to and comes_from set of the receivers. Note: jp, jr and ret are already done in update_next_block()
5.079144
4.683015
1.084588
if i < 0: i = 0 if self.lock: return True regs = list(regs) # make a copy if top is None: top = len(self) else: top -= 1 for ii in range(i, top): for r in self.mem[ii].requires: if r ...
def is_used(self, regs, i, top=None)
Checks whether any of the given regs are required from the given point to the end or not.
3.606932
3.285441
1.097853
if is_register(regs): regs = set(single_registers(regs)) else: regs = set(single_registers(x) for x in regs) return not regs.intersection(self.requires(i, end_))
def safe_to_write(self, regs, i=0, end_=0)
Given a list of registers (8 or 16 bits) returns a list of them that are safe to modify from the given index until the position given which, if omitted, defaults to the end of the block. :param regs: register or iterable of registers (8 or 16 bit one) :param i: initial position of the bl...
5.216303
6.661487
0.783054
if i < 0: i = 0 end_ = len(self) if end_ is None or end_ > len(self) else end_ regs = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'l', 'i', 'ixh', 'ixl', 'iyh', 'iyl', 'sp'} result = [] for ii in range(i, end_): for r in self.mem[ii].requires: ...
def requires(self, i=0, end_=None)
Returns a list of registers and variables this block requires. By default checks from the beginning (i = 0). :param i: initial position of the block to examine :param end_: final position to examine :returns: registers safe to write
2.813717
2.758572
1.019991
regs = {'a', 'b', 'c', 'd', 'e', 'f', 'h', 'l', 'i', 'ixh', 'ixl', 'iyh', 'iyl', 'sp'} top = len(self) result = [] for ii in range(i, top): for r in self.mem[ii].destroys: if r in regs: result.append(r) regs.re...
def destroys(self, i=0)
Returns a list of registers this block destroys By default checks from the beginning (i = 0).
3.837655
3.24285
1.183421
self.mem[a], self.mem[b] = self.mem[b], self.mem[a] self.asm[a], self.asm[b] = self.asm[b], self.asm[a]
def swap(self, a, b)
Swaps mem positions a and b
2.272859
1.95182
1.164482
if len(self) and self.mem[-1].inst == 'call' and self.mem[-1].condition_flag is None: for block in self.calls: if block.is_used(regs, 0): return True d = block.destroys() if not len([x for x in regs if x not in d]): ...
def goes_requires(self, regs)
Returns whether any of the goes_to block requires any of the given registers.
6.209329
5.864368
1.058823
for i in range(len(self)): if self.mem[i].is_label and self.mem[i].inst == label: return i return None
def get_label_idx(self, label)
Returns the index of a label. Returns None if not found.
5.950801
5.039374
1.180861
for i in range(len(self)): if not self.mem[i].is_label: return self.mem[i] return None
def get_first_non_label_instruction(self)
Returns the memcell of the given block, which is not a LABEL.
3.799964
3.2131
1.182647
''' Returns pop sequence for 16 bits operands 1st operand in HL, 2nd operand in DE You can swap operators extraction order by setting reversed to True. If no_exaf = True => No bits flags in A' will be used. This saves two bytes. ''' output = [] if op2 is not No...
def _str_oper(op1, op2=None, reversed=False, no_exaf=False)
Returns pop sequence for 16 bits operands 1st operand in HL, 2nd operand in DE You can swap operators extraction order by setting reversed to True. If no_exaf = True => No bits flags in A' will be used. This saves two bytes.
3.515963
2.515747
1.397582
''' Outputs a FREEMEM sequence for 1 or 2 ops ''' if not tmp1 and not tmp2: return [] output = [] if tmp1 and tmp2: output.append('pop de') output.append('ex (sp), hl') output.append('push de') output.append('call __MEM_FREE') output.append('pop hl') ...
def _free_sequence(tmp1, tmp2=False)
Outputs a FREEMEM sequence for 1 or 2 ops
5.933164
4.710539
1.259551
''' Compares & pops top 2 strings out of the stack. Temporal values are freed from memory. (a$ != b$) ''' (tmp1, tmp2, output) = _str_oper(ins.quad[2], ins.quad[3]) output.append('call __STRNE') output.append('push af') REQUIRES.add('string.asm') return output
def _nestr(ins)
Compares & pops top 2 strings out of the stack. Temporal values are freed from memory. (a$ != b$)
25.52783
8.443302
3.023441
''' Returns string length ''' (tmp1, output) = _str_oper(ins.quad[2], no_exaf=True) if tmp1: output.append('push hl') output.append('call __STRLEN') output.extend(_free_sequence(tmp1)) output.append('push hl') REQUIRES.add('strlen.asm') return output
def _lenstr(ins)
Returns string length
17.794804
17.96718
0.990406
if func is not None and len(operands) == 1: # Try constant-folding if is_number(operands[0]) or is_string(operands[0]): # e.g. ABS(-5) return SymbolNUMBER(func(operands[0].value), type_=type_, lineno=lineno) return cls(lineno, fname, type_, *operands)
def make_node(cls, lineno, fname, func=None, type_=None, *operands)
Creates a node for a unary operation. E.g. -x or LEN(a$) Parameters: -func: function used on constant folding when possible -type_: the resulting type (by default, the same as the argument). For example, for LEN (str$), result type is 'u16' and arg type i...
5.767578
5.164939
1.116679
output = [] if op2 is not None and reversed_: tmp = op1 op1 = op2 op2 = tmp op = op1 indirect = (op[0] == '*') if indirect: op = op[1:] immediate = (op[0] == '#') if immediate: op = op[1:] if is_int(op): op = int(op) if in...
def _8bit_oper(op1, op2=None, reversed_=False)
Returns pop sequence for 8 bits operands 1st operand in H, 2nd operand in A (accumulator) For some operations (like comparisons), you can swap operands extraction by setting reversed = True
2.029652
2.036244
0.996762
op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) output = _8bit_oper(op1) if op2 == 0: # Nothing to add: A + 0 = A output.append('push af') return output op2 = int8(op2) if op2 == 1: # Adding...
def _add8(ins)
Pops last 2 bytes from the stack and adds them. Then push the result onto the stack. Optimizations: * If any of the operands is ZERO, then do NOTHING: A + 0 = 0 + A = A * If any of the operands is 1, then INC is used * If any of the operands is -1 (255), then DEC is ...
3.496855
3.333503
1.049003
op1, op2 = tuple(ins.quad[2:]) if is_int(op2): # 2nd operand op2 = int8(op2) output = _8bit_oper(op1) if op2 == 0: output.append('push af') return output # A - 0 = A op2 = int8(op2) if op2 == 1: # A - 1 == DEC A output.appen...
def _sub8(ins)
Pops last 2 bytes from the stack and subtract them. Then push the result onto the stack. Top-1 of the stack is subtracted Top _sub8 t1, a, b === t1 <-- a - b Optimizations: * If 2nd op is ZERO, then do NOTHING: A - 0 = A * If 1st operand is 0, then just do a NEG * If...
3.825367
3.444964
1.110423
op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) output = _8bit_oper(op1) if op2 == 1: # A * 1 = 1 * A = A output.append('push af') return output if op2 == 0: output.append('xor a') ...
def _mul8(ins)
Multiplies 2 las values from the stack. Optimizations: * If any of the ops is ZERO, then do A = 0 ==> XOR A, cause A * 0 = 0 * A = 0 * If any ot the ops is ONE, do NOTHING A * 1 = 1 * A = A
4.149419
3.879815
1.069489
op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op2 = int8(op2) output = _8bit_oper(op1) if op2 == 1: output.append('push af') return output if op2 == 2: output.append('srl a') output.append('push af') return outpu...
def _divu8(ins)
Divides 2 8bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd op is 1 then do nothing * If 2nd op is 2 then Shift Right Logical
5.693489
5.536014
1.028446
output = _8bit_oper(ins.quad[2], ins.quad[3]) output.append('cp h') output.append('sbc a, a') output.append('push af') return output
def _ltu8(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand < 2nd operand (top of the stack). Pushes 0 if False, 1 if True. 8 bit unsigned version
13.348668
16.156973
0.826186