code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
result = True # Check for functions defined after calls (parametres, etc) for id_, params, lineno in global_.FUNCTION_CALLS: result = result and check_call_arguments(lineno, id_, params) return result
def check_pending_calls()
Calls the above function for each pending call of the current scope level
19.839251
16.863346
1.176472
result = True visited = set() pending = [ast] while pending: node = pending.pop() if node is None or node in visited: # Avoid recursive infinite-loop continue visited.add(node) for x in node.children: pending.append(x) if node.token != 'VAR' or (node.token == 'VAR' and node.class_ is not CLASS.unknown): continue tmp = global_.SYMBOL_TABLE.get_entry(node.name) if tmp is None or tmp.class_ is CLASS.unknown: syntax_error(node.lineno, 'Undeclared identifier "%s"' % node.name) else: assert tmp.class_ == CLASS.label node.to_label(node) result = result and tmp is not None return result
def check_pending_labels(ast)
Iteratively traverses the node looking for ID with no class set, marks them as labels, and check they've been declared. This way we avoid stack overflow for high line-numbered listings.
4.580366
4.489927
1.020143
if isinstance(lbl, float): if lbl == int(lbl): id_ = str(int(lbl)) else: syntax_error(lineno, 'Line numbers must be integers.') return None else: id_ = lbl return global_.SYMBOL_TABLE.access_label(id_, lineno)
def check_and_make_label(lbl, lineno)
Checks if the given label (or line number) is valid and, if so, returns a label object. :param lbl: Line number of label (string) :param lineno: Line number in the basic source code for error reporting :return: Label object or None if error.
5.699485
5.519907
1.032533
from symbols.symbol_ import Symbol for sym in symbols: if sym is None: continue if not isinstance(sym, Symbol): return False if sym.token == 'NOP': continue if sym.token == 'BLOCK': if not is_null(*sym.children): return False continue return False return True
def is_null(*symbols)
True if no nodes or all the given nodes are either None, NOP or empty blocks. For blocks this applies recursively
4.033003
3.518943
1.146084
from symbols.symbol_ import Symbol assert all(isinstance(x, Symbol) for x in symbols) for sym in symbols: if sym.token != token: return False return True
def is_SYMBOL(token, *symbols)
Returns True if ALL of the given argument are AST nodes of the given token (e.g. 'BINARY')
5.439189
5.22491
1.041011
return is_SYMBOL('VAR', *p) and all(x.class_ == CLASS.const for x in p)
def is_const(*p)
A constant in the program, like CONST a = 5
18.349892
14.884018
1.232859
return all(is_CONST(x) or is_number(x) or is_const(x) for x in p)
def is_static(*p)
A static value (does not change at runtime) which is known at compile time
6.313923
7.493677
0.842567
try: for i in p: if i.token != 'NUMBER' and (i.token != 'ID' or i.class_ != CLASS.const): return False return True except: pass return False
def is_number(*p)
Returns True if ALL of the arguments are AST nodes containing NUMBER or numeric CONSTANTS
5.598751
4.876421
1.148127
from symbols.type_ import Type try: for i in p: if not i.type_.is_basic or not Type.is_unsigned(i.type_): return False return True except: pass return False
def is_unsigned(*p)
Returns false unless all types in p are unsigned
5.478511
5.368435
1.020504
try: for i in p: if i.type_ != type_: return False return True except: pass return False
def is_type(type_, *p)
True if all args have the same type
4.586682
4.084093
1.12306
for i in p: if i.scope == SCOPE.global_ and i.is_basic and \ i.type_ != Type.string: return False return True except: pass return False
def is_dynamic(*p): # TODO: Explain this better from symbols.type_ import Type try
True if all args are dynamic (e.g. Strings, dynamic arrays, etc) The use a ptr (ref) and it might change during runtime.
8.289321
7.917642
1.046943
import symbols return all(isinstance(x, symbols.FUNCTION) for x in p)
def is_callable(*p)
True if all the args are functions and / or subroutines
11.754319
10.059525
1.168477
if is_LABEL(block) and block.accessed: return True for child in block.children: if not is_callable(child) and is_block_accessed(child): return True return False
def is_block_accessed(block)
Returns True if a block is "accessed". A block of code is accessed if it has a LABEL and it is used in a GOTO, GO SUB or @address access :param block: A block of code (AST node) :return: True / False depending if it has labels accessed or not
4.327068
5.217452
0.829345
from symbols.type_ import SymbolBASICTYPE as BASICTYPE from symbols.type_ import Type as TYPE from symbols.type_ import SymbolTYPE if a is None or b is None: return None if not isinstance(a, SymbolTYPE): a = a.type_ if not isinstance(b, SymbolTYPE): b = b.type_ if a == b: # Both types are the same? return a # Returns it if a == TYPE.unknown and b == TYPE.unknown: return BASICTYPE(global_.DEFAULT_TYPE) if a == TYPE.unknown: return b if b == TYPE.unknown: return a # TODO: This will removed / expanded in the future assert a.is_basic assert b.is_basic types = (a, b) if TYPE.float_ in types: return TYPE.float_ if TYPE.fixed in types: return TYPE.fixed if TYPE.string in types: # TODO: Check this ?? return TYPE.unknown result = a if a.size > b.size else b if not TYPE.is_unsigned(a) or not TYPE.is_unsigned(b): result = TYPE.to_signed(result) return result
def common_type(a, b)
Returns a type which is common for both a and b types. Returns None if no common types allowed.
4.223965
4.116961
1.025991
for m in memory: m = m.rstrip('\r\n\t ') # Ensures no trailing newlines (might with upon includes) if m and m[0] == '#': # Preprocessor directive? if ofile is None: print(m) else: ofile.write('%s\n' % m) continue # Prints a 4 spaces "tab" for non labels if m and ':' not in m: if ofile is None: print(' '), else: ofile.write('\t') if ofile is None: print(m) else: ofile.write('%s\n' % m)
def output(memory, ofile=None)
Filters the output removing useless preprocessor #directives and writes it to the given file or to the screen if no file is passed
4.536332
4.210233
1.077454
''' Returns pop sequence for 16 bits operands 1st operand in HL, 2nd operand in DE For subtraction, division, etc. you can swap operators extraction order by setting reversed to True ''' output = [] if op1 is not None: op1 = str(op1) # always to str if op2 is not None: op2 = str(op2) # always to str if op2 is not None and reversed: op1, op2 = op2, op1 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 indirect: output.append('ld hl, (%i)' % op) else: output.append('ld hl, %i' % int16(op)) else: if immediate: if indirect: output.append('ld hl, (%s)' % op) else: output.append('ld hl, %s' % op) else: if op[0] == '_': output.append('ld hl, (%s)' % op) else: output.append('pop hl') if indirect: output.append('ld a, (hl)') output.append('inc hl') output.append('ld h, (hl)') output.append('ld l, a') if op2 is None: return output if not reversed: tmp = output output = [] op = op2 indirect = (op[0] == '*') if indirect: op = op[1:] immediate = (op[0] == '#') if immediate: op = op[1:] if is_int(op): op = int(op) if indirect: output.append('ld de, (%i)' % op) else: output.append('ld de, %i' % int16(op)) else: if immediate: output.append('ld de, %s' % op) else: if op[0] == '_': output.append('ld de, (%s)' % op) else: output.append('pop de') if indirect: output.append('call __LOAD_DE_DE') # DE = (DE) REQUIRES.add('lddede.asm') if not reversed: output.extend(tmp) return output
def _16bit_oper(op1, op2=None, reversed=False)
Returns pop sequence for 16 bits operands 1st operand in HL, 2nd operand in DE For subtraction, division, etc. you can swap operators extraction order by setting reversed to True
2.761564
2.208149
1.250624
''' 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 < 4, then INC is used * If any of the operands is > (65531) (-4), then DEC is used ''' op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) op2 = int16(op2) output = _16bit_oper(op1) if op2 == 0: output.append('push hl') return output # ADD HL, 0 => NOTHING if op2 < 4: output.extend(['inc hl'] * op2) # ADD HL, 2 ==> inc hl; inc hl output.append('push hl') return output if op2 > 65531: # (between -4 and 0) output.extend(['dec hl'] * (0x10000 - op2)) output.append('push hl') return output output.append('ld de, %i' % op2) output.append('add hl, de') output.append('push hl') return output if op2[0] == '_': # stack optimization op1, op2 = op2, op1 output = _16bit_oper(op1, op2) output.append('add hl, de') output.append('push hl') return output
def _add16(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 < 4, then INC is used * If any of the operands is > (65531) (-4), then DEC is used
4.563395
2.851871
1.600141
''' Pops last 2 words from the stack and subtract them. Then push the result onto the stack. Top of the stack is subtracted Top -1 Optimizations: * If 2nd op is ZERO, then do NOTHING: A - 0 = A * If any of the operands is < 4, then DEC is used * If any of the operands is > 65531 (-4..-1), then INC is used ''' op1, op2 = tuple(ins.quad[2:4]) if is_int(op2): op = int16(op2) output = _16bit_oper(op1) if op == 0: output.append('push hl') return output if op < 4: output.extend(['dec hl'] * op) output.append('push hl') return output if op > 65531: output.extend(['inc hl'] * (0x10000 - op)) output.append('push hl') return output output.append('ld de, -%i' % op) output.append('add hl, de') output.append('push hl') return output if op2[0] == '_': # Optimization when 2nd operand is an id rev = True op1, op2 = op2, op1 else: rev = False output = _16bit_oper(op1, op2, rev) output.append('or a') output.append('sbc hl, de') output.append('push hl') return output
def _sub16(ins)
Pops last 2 words from the stack and subtract them. Then push the result onto the stack. Top of the stack is subtracted Top -1 Optimizations: * If 2nd op is ZERO, then do NOTHING: A - 0 = A * If any of the operands is < 4, then DEC is used * If any of the operands is > 65531 (-4..-1), then INC is used
5.297718
2.761054
1.91873
''' Multiplies tow last 16bit values on top of the stack and and returns the value on top of 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 * If B is 2^n and B < 16 => Shift Right n ''' op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: # If any of the operands is constant op1, op2 = _int_ops(op1, op2) # put the constant one the 2nd output = _16bit_oper(op1) if op2 == 0: # A * 0 = 0 * A = 0 if op1[0] in ('_', '$'): output = [] # Optimization: Discard previous op if not from the stack output.append('ld hl, 0') output.append('push hl') return output if op2 == 1: # A * 1 = 1 * A == A => Do nothing output.append('push hl') return output if op2 == 0xFFFF: # This is the same as (-1) output.append('call __NEGHL') output.append('push hl') REQUIRES.add('neg16.asm') return output if is_2n(op2) and log2(op2) < 4: output.extend(['add hl, hl'] * int(log2(op2))) output.append('push hl') return output output.append('ld de, %i' % op2) else: if op2[0] == '_': # stack optimization op1, op2 = op2, op1 output = _16bit_oper(op1, op2) output.append('call __MUL16_FAST') # Inmmediate output.append('push hl') REQUIRES.add('mul16.asm') return output
def _mul16(ins)
Multiplies tow last 16bit values on top of the stack and and returns the value on top of 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 * If B is 2^n and B < 16 => Shift Right n
6.256519
3.718404
1.682582
''' Divides 2 16bit 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 ''' op1, op2 = tuple(ins.quad[2:]) if is_int(op1) and int(op1) == 0: # 0 / A = 0 if op2[0] in ('_', '$'): output = [] # Optimization: Discard previous op if not from the stack else: output = _16bit_oper(op2) # Normalize stack output.append('ld hl, 0') output.append('push hl') return output if is_int(op2): op = int16(op2) output = _16bit_oper(op1) if op2 == 0: # A * 0 = 0 * A = 0 if op1[0] in ('_', '$'): output = [] # Optimization: Discard previous op if not from the stack output.append('ld hl, 0') output.append('push hl') return output if op == 1: output.append('push hl') return output if op == 2: output.append('srl h') output.append('rr l') output.append('push hl') return output output.append('ld de, %i' % op) else: if op2[0] == '_': # Optimization when 2nd operand is an id rev = True op1, op2 = op2, op1 else: rev = False output = _16bit_oper(op1, op2, rev) output.append('call __DIVU16') output.append('push hl') REQUIRES.add('div16.asm') return output
def _divu16(ins)
Divides 2 16bit 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
4.778126
3.8581
1.238466
''' Reminder of div. 2 16bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd operand is 1 => Return 0 * If 2nd operand = 2^n => do AND (2^n - 1) ''' op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op2 = int16(op2) output = _16bit_oper(op1) if op2 == 1: if op2[0] in ('_', '$'): output = [] # Optimization: Discard previous op if not from the stack output.append('ld hl, 0') output.append('push hl') return output if is_2n(op2): k = op2 - 1 if op2 > 255: # only affects H output.append('ld a, h') output.append('and %i' % (k >> 8)) output.append('ld h, a') else: output.append('ld h, 0') # High part goes 0 output.append('ld a, l') output.append('and %i' % (k % 0xFF)) output.append('ld l, a') output.append('push hl') return output output.append('ld de, %i' % op2) else: output = _16bit_oper(op1, op2) output.append('call __MODU16') output.append('push hl') REQUIRES.add('div16.asm') return output
def _modu16(ins)
Reminder of div. 2 16bit unsigned integers. The result is pushed onto the stack. Optimizations: * If 2nd operand is 1 => Return 0 * If 2nd operand = 2^n => do AND (2^n - 1)
6.02221
4.138873
1.455036
''' 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. 16 bit unsigned version ''' output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('or a') output.append('sbc hl, de') output.append('sbc a, a') output.append('push af') return output
def _ltu16(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. 16 bit unsigned version
10.252542
3.990804
2.569042
''' 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. 16 bit signed version ''' output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('call __LTI16') output.append('push af') REQUIRES.add('lti16.asm') return output
def _lti16(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. 16 bit signed version
12.528338
4.794461
2.613086
''' 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. 16 bit unsigned version ''' output = _16bit_oper(ins.quad[2], ins.quad[3], reversed=True) output.append('or a') output.append('sbc hl, de') output.append('sbc a, a') output.append('push af') return output
def _gtu16(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. 16 bit unsigned version
10.488056
4.194559
2.500396
''' 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. 16 bit signed version ''' output = _16bit_oper(ins.quad[2], ins.quad[3], reversed=True) output.append('call __LTI16') output.append('push af') REQUIRES.add('lti16.asm') return output
def _gti16(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. 16 bit signed version
14.571978
5.407968
2.694538
''' 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. 16 bit unsigned version ''' output = _16bit_oper(ins.quad[2], ins.quad[3], reversed=True) output.append('or a') output.append('sbc hl, de') # Carry if A > B output.append('ccf') # Negates the result => Carry if A <= B output.append('sbc a, a') output.append('push af') return output
def _leu16(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. 16 bit unsigned version
11.124269
5.155908
2.157577
''' 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. 16 bit signed version ''' output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('call __LEI16') output.append('push af') REQUIRES.add('lei16.asm') return output
def _lei16(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. 16 bit signed version
13.400765
4.78688
2.799478
''' 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. 16 bit unsigned version ''' output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('or a') output.append('sbc hl, de') output.append('ccf') output.append('sbc a, a') output.append('push af') return output
def _geu16(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. 16 bit unsigned version
10.259347
4.141948
2.476938
''' 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. 16 bit un/signed version ''' output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('call __EQ16') output.append('push af') REQUIRES.add('eq16.asm') return output
def _eq16(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. 16 bit un/signed version
14.734719
5.086755
2.896684
''' 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. 16 bit un/signed version ''' output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('or a') # Resets carry flag output.append('sbc hl, de') output.append('ld a, h') output.append('or l') output.append('push af') return output
def _ne16(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. 16 bit un/signed version
11.726015
4.585811
2.557021
''' Compares & pops top 2 operands out of the stack, and checks if the 1st operand OR (logical) 2nd operand (top of the stack), pushes 0 if False, 1 if True. 16 bit un/signed version Optimizations: If any of the operators are constants: Returns either 0 or the other operand ''' op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) if op2 == 0: output = _16bit_oper(op1) output.append('ld a, h') output.append('or l') # Convert x to Boolean output.append('push af') return output # X or False = X output = _16bit_oper(op1) output.append('ld a, 0FFh') # X or True = True output.append('push af') return output output = _16bit_oper(ins.quad[2], ins.quad[3]) output.append('ld a, h') output.append('or l') output.append('or d') output.append('or e') output.append('push af') return output
def _or16(ins)
Compares & pops top 2 operands out of the stack, and checks if the 1st operand OR (logical) 2nd operand (top of the stack), pushes 0 if False, 1 if True. 16 bit un/signed version Optimizations: If any of the operators are constants: Returns either 0 or the other operand
6.112658
3.026862
2.01947
''' Pops top 2 operands out of the stack, and performs 1st operand OR (bitwise) 2nd operand (top of the stack), pushes result (16 bit in HL). 16 bit un/signed version Optimizations: If any of the operators are constants: Returns either 0 or the other operand ''' op1, op2 = tuple(ins.quad[2:]) if _int_ops(op1, op2) is not None: op1, op2 = _int_ops(op1, op2) output = _16bit_oper(op1) if op2 == 0: # X | 0 = X output.append('push hl') return output if op2 == 0xFFFF: # X & 0xFFFF = 0xFFFF output.append('ld hl, 0FFFFh') output.append('push hl') return output output = _16bit_oper(op1, op2) output.append('call __BOR16') output.append('push hl') REQUIRES.add('bor16.asm') return output
def _bor16(ins)
Pops top 2 operands out of the stack, and performs 1st operand OR (bitwise) 2nd operand (top of the stack), pushes result (16 bit in HL). 16 bit un/signed version Optimizations: If any of the operators are constants: Returns either 0 or the other operand
7.608874
3.317036
2.293877
''' Negates top (Logical NOT) of the stack (16 bits in HL) ''' output = _16bit_oper(ins.quad[2]) output.append('ld a, h') output.append('or l') output.append('sub 1') output.append('sbc a, a') output.append('push af') return output
def _not16(ins)
Negates top (Logical NOT) of the stack (16 bits in HL)
12.486103
7.087039
1.761822
''' Negates top (Bitwise NOT) of the stack (16 bits in HL) ''' output = _16bit_oper(ins.quad[2]) output.append('call __BNOT16') output.append('push hl') REQUIRES.add('bnot16.asm') return output
def _bnot16(ins)
Negates top (Bitwise NOT) of the stack (16 bits in HL)
19.587534
11.048168
1.772921
''' Negates top of the stack (16 bits in HL) ''' output = _16bit_oper(ins.quad[2]) output.append('call __NEGHL') output.append('push hl') REQUIRES.add('neg16.asm') return output
def _neg16(ins)
Negates top of the stack (16 bits in HL)
21.85759
16.092123
1.358279
''' Absolute value of top of the stack (16 bits in HL) ''' output = _16bit_oper(ins.quad[2]) output.append('call __ABS16') output.append('push hl') REQUIRES.add('abs16.asm') return output
def _abs16(ins)
Absolute value of top of the stack (16 bits in HL)
17.730482
13.62318
1.301494
''' Logical right shift 16bit unsigned integer. The result is pushed onto the stack. Optimizations: * If 2nd op is 0 then do nothing * If 2nd op is 1 Shift Right Arithmetic ''' op1, op2 = tuple(ins.quad[2:]) if is_int(op2): op = int16(op2) if op == 0: return [] output = _16bit_oper(op1) if op == 1: output.append('srl h') output.append('rr l') output.append('push hl') return output output.append('ld b, %i' % op) else: output = _8bit_oper(op2) output.append('ld b, a') output.extend(_16bit_oper(op1)) label = tmp_label() output.append('%s:' % label) output.append('srl h') output.append('rr l') output.append('djnz %s' % label) output.append('push hl') return output
def _shru16(ins)
Logical right shift 16bit unsigned integer. The result is pushed onto the stack. Optimizations: * If 2nd op is 0 then do nothing * If 2nd op is 1 Shift Right Arithmetic
5.457038
3.649913
1.495115
self.out(self.LH(len(bytes_) + 1)) # + 1 for CHECKSUM byte checksum = 0 for i in bytes_: checksum ^= (int(i) & 0xFF) self.out(i) self.out(checksum)
def standard_block(self, bytes_)
Adds a standard block of bytes. For TAP files, it's just the Low + Hi byte plus the content (here, the bytes plus the checksum)
5.773748
4.907112
1.176608
encodings = ['utf-8-sig', 'cp1252'] with open(fname, 'rb') as f: content = bytes(f.read()) for i in encodings: try: result = content.decode(i) if six.PY2: result = result.encode('utf-8') return result except UnicodeDecodeError: pass global_.FILENAME = fname errmsg.syntax_error(1, 'Invalid file encoding. Use one of: %s' % ', '.join(encodings)) return ''
def read_txt_file(fname)
Reads a txt file, regardless of its encoding
3.990541
3.88797
1.026382
if six.PY2 or 't' not in mode: kwargs = {} else: kwargs = {'encoding': encoding} return open(fname, mode, **kwargs)
def open_file(fname, mode='rb', encoding='utf-8')
An open() wrapper for PY2 and PY3 which allows encoding :param fname: file name (string) :param mode: file mode (string) optional :param encoding: optional encoding (string). Ignored in python2 or if not in text mode :return: an open file handle
3.886946
3.98787
0.974692
str_num = (str_num or "").strip().upper() if not str_num: return None base = 10 if str_num.startswith('0X'): base = 16 str_num = str_num[2:] if str_num.endswith('H'): base = 16 str_num = str_num[:-1] if str_num.startswith('$'): base = 16 str_num = str_num[1:] try: return int(str_num, base) except ValueError: return None
def parse_int(str_num)
Given an integer number, return its value, or None if it could not be parsed. Allowed formats: DECIMAL, HEXA (0xnnn, $nnnn or nnnnh) :param str_num: (string) the number to be parsed :return: an integer number or None if it could not be parsedd
1.901065
1.791363
1.06124
if not isinstance(l, list): l = [l] self.output.extend([int(i) & 0xFF for i in l])
def out(self, l)
Adds a list of bytes to the output string
4.491113
3.85352
1.165457
self.out(self.BLOCK_STANDARD) # Standard block ID self.out(self.LH(1000)) # 1000 ms standard pause self.out(self.LH(len(_bytes) + 1)) # + 1 for CHECKSUM byte checksum = 0 for i in _bytes: checksum ^= (int(i) & 0xFF) self.out(i) self.out(checksum)
def standard_block(self, _bytes)
Adds a standard block of bytes
5.259664
5.235721
1.004573
with open(fname, 'wb') as f: f.write(self.output)
def dump(self, fname)
Saves TZX file to fname
3.91641
3.989751
0.981618
title = (title + 10 * ' ')[:10] # Padd it with spaces title_bytes = [ord(i) for i in title] # Convert it to bytes _bytes = [self.BLOCK_TYPE_HEADER, _type] + title_bytes + self.LH(length) + self.LH(param1) + self.LH(param2) self.standard_block(_bytes)
def save_header(self, _type, title, length, param1, param2)
Saves a generic standard header: type: 00 -- Program 01 -- Number Array 02 -- Char Array 03 -- Code title: Name title. Will be truncated to 10 chars and padded with spaces if necessary. length: Data length (in bytes) of the next block. param1: For CODE -> Start address. For PROGRAM -> Autostart line (>=32768 for no autostart) For DATA (02 & 03) high byte of param1 have the variable name. param2: For CODE -> 32768 For PROGRAM -> Start of the Variable area relative to program Start (Length of basic in bytes) For DATA (02 & 03) NOT USED Info taken from: http://www.worldofspectrum.org/faq/reference/48kreference.htm#TapeDataStructure
5.532807
5.355974
1.033016
self.save_header(self.HEADER_TYPE_CODE, title, length, param1=addr, param2=32768)
def standard_bytes_header(self, title, addr, length)
Generates a standard header block of CODE type
10.355989
8.404181
1.232242
self.save_header(self.HEADER_TYPE_BASIC, title, length, param1=line, param2=length)
def standard_program_header(self, title, length, line=32768)
Generates a standard header block of PROGRAM type
10.514177
10.463333
1.004859
self.standard_bytes_header(title, addr, len(_bytes)) _bytes = [self.BLOCK_TYPE_DATA] + [(int(x) & 0xFF) for x in _bytes] # & 0xFF truncates to bytes self.standard_block(_bytes)
def save_code(self, title, addr, _bytes)
Saves the given bytes as code. If bytes are strings, its chars will be converted to bytes
7.062387
7.554525
0.934855
self.standard_program_header(title, len(bytes), line) bytes = [self.BLOCK_TYPE_DATA] + [(int(x) & 0xFF) for x in bytes] # & 0xFF truncates to bytes self.standard_block(bytes)
def save_program(self, title, bytes, line=32768)
Saves the given bytes as a BASIC program.
6.755334
6.656813
1.0148
if lower is None or upper is None or s is None: return None if not check_type(lineno, Type.string, s): return None lo = up = None base = NUMBER(api.config.OPTIONS.string_base.value, lineno=lineno) lower = TYPECAST.make_node(gl.SYMBOL_TABLE.basic_types[gl.STR_INDEX_TYPE], BINARY.make_node('MINUS', lower, base, lineno=lineno, func=lambda x, y: x - y), lineno) upper = TYPECAST.make_node(gl.SYMBOL_TABLE.basic_types[gl.STR_INDEX_TYPE], BINARY.make_node('MINUS', upper, base, lineno=lineno, func=lambda x, y: x - y), lineno) if lower is None or upper is None: return None if is_number(lower): lo = lower.value if lo < gl.MIN_STRSLICE_IDX: lower.value = lo = gl.MIN_STRSLICE_IDX if is_number(upper): up = upper.value if up > gl.MAX_STRSLICE_IDX: upper.value = up = gl.MAX_STRSLICE_IDX if is_number(lower, upper): if lo > up: return STRING('', lineno) if s.token == 'STRING': # A constant string? Recalculate it now up += 1 st = s.value.ljust(up) # Procrustean filled (right) return STRING(st[lo:up], lineno) # a$(0 TO INF.) = a$ if lo == gl.MIN_STRSLICE_IDX and up == gl.MAX_STRSLICE_IDX: return s return cls(s, lower, upper, lineno)
def make_node(cls, lineno, s, lower, upper)
Creates a node for a string slice. S is the string expression Tree. Lower and upper are the bounds, if lower & upper are constants, and s is also constant, then a string constant is returned. If lower > upper, an empty string is returned.
4.77586
4.532634
1.053661
global ASMS global ASMCOUNT global AT_END global FLAG_end_emitted global FLAG_use_function_exit __common.init() ASMS = {} ASMCOUNT = 0 AT_END = [] FLAG_use_function_exit = False FLAG_end_emitted = False # Default code ORG OPTIONS.add_option('org', int, 32768) # Default HEAP SIZE (Dynamic memory) in bytes OPTIONS.add_option('heap_size', int, 4768) # A bit more than 4K # Labels for HEAP START (might not be used if not needed) OPTIONS.add_option('heap_start_label', str, 'ZXBASIC_MEM_HEAP') # Labels for HEAP SIZE (might not be used if not needed) OPTIONS.add_option('heap_size_label', str, 'ZXBASIC_HEAP_SIZE') # Flag for headerless mode (No prologue / epilogue) OPTIONS.add_option('headerless', bool, False)
def init()
Initializes this module
6.431042
6.445052
0.997826
output = [] if stype in ('i8', 'u8'): return [] if is_int_type(stype): output.append('ld a, l') elif stype == 'f16': output.append('ld a, e') elif stype == 'f': # Converts C ED LH to byte output.append('call __FTOU32REG') output.append('ld a, l') REQUIRES.add('ftou32reg.asm') return output
def to_byte(stype)
Returns the instruction sequence for converting from the given type to byte.
8.598422
8.393352
1.024432
output = [] # List of instructions if stype == 'u8': # Byte to word output.append('ld l, a') output.append('ld h, 0') elif stype == 'i8': # Signed byte to word output.append('ld l, a') output.append('add a, a') output.append('sbc a, a') output.append('ld h, a') elif stype == 'f16': # Must MOVE HL into DE output.append('ex de, hl') elif stype == 'f': output.append('call __FTOU32REG') REQUIRES.add('ftou32reg.asm') return output
def to_word(stype)
Returns the instruction sequence for converting the given type stored in DE,HL to word (unsigned) HL.
5.721276
5.414023
1.056751
output = [] # List of instructions if is_int_type(stype): output = to_word(stype) output.append('ex de, hl') output.append('ld hl, 0') # 'Truncate' the fixed point elif stype == 'f': output.append('call __FTOF16REG') REQUIRES.add('ftof16reg.asm') return output
def to_fixed(stype)
Returns the instruction sequence for converting the given type stored in DE,HL to fixed DE,HL.
14.043277
12.624643
1.11237
output = [] # List of instructions if stype == 'f': return [] # Nothing to do if stype == 'f16': output.append('call __F16TOFREG') REQUIRES.add('f16tofreg.asm') return output # If we reach this point, it's an integer type if stype == 'u8': output.append('call __U8TOFREG') elif stype == 'i8': output.append('call __I8TOFREG') else: output = to_long(stype) if stype in ('i16', 'i32'): output.append('call __I32TOFREG') else: output.append('call __U32TOFREG') REQUIRES.add('u32tofreg.asm') return output
def to_float(stype)
Returns the instruction sequence for converting the given type stored in DE,HL to fixed DE,HL.
3.283669
3.180398
1.032471
global FLAG_end_emitted output = _16bit_oper(ins.quad[1]) output.append('ld b, h') output.append('ld c, l') if FLAG_end_emitted: return output + ['jp %s' % END_LABEL] FLAG_end_emitted = True output.append('%s:' % END_LABEL) if OPTIONS.headerless.value: return output + ['ret'] output.append('di') output.append('ld hl, (%s)' % CALL_BACK) output.append('ld sp, hl') output.append('exx') output.append('pop hl') output.append('exx') output.append('pop iy') output.append('pop ix') output.append('ei') output.append('ret') output.append('%s:' % CALL_BACK) output.append('DEFW 0') return output
def _end(ins)
Outputs the ending sequence
5.605093
5.609806
0.99916
output = [] t = ins.quad[1] q = eval(ins.quad[2]) if t in ('i8', 'u8'): size = 'B' elif t in ('i16', 'u16'): size = 'W' elif t in ('i32', 'u32'): size = 'W' z = list() for expr in ins.quad[2]: z.extend(['(%s) & 0xFFFF' % expr, '(%s) >> 16' % expr]) q = z elif t == 'str': size = "B" q = ['"%s"' % x.replace('"', '""') for x in q] elif t == 'f': dat_ = [api.fp.immediate_float(float(x)) for x in q] for x in dat_: output.extend(['DEFB %s' % x[0], 'DEFW %s, %s' % (x[1], x[2])]) return output else: raise InvalidIC(ins.quad, 'Unimplemented data size %s for %s' % (t, q)) for x in q: output.append('DEF%s %s' % (size, x)) return output
def _data(ins)
Defines a data item (binary). It's just a constant expression to be converted do binary data "as is" 1st parameter is the type-size (u8 or i8 for byte, u16 or i16 for word, etc) 2nd parameter is the list of expressions. All of them will be converted to the type required.
3.939739
3.647851
1.080017
output = [] output.append('%s:' % ins.quad[1]) output.append('DEFB %s' % ((int(ins.quad[2]) - 1) * '00, ' + '00')) return output
def _var(ins)
Defines a memory variable.
10.081974
9.333661
1.080174
output = [] output.append('%s:' % ins.quad[1]) q = eval(ins.quad[3]) if ins.quad[2] in ('i8', 'u8'): size = 'B' elif ins.quad[2] in ('i16', 'u16'): size = 'W' elif ins.quad[2] in ('i32', 'u32'): size = 'W' z = list() for expr in q: z.extend(['(%s) & 0xFFFF' % expr, '(%s) >> 16' % expr]) q = z else: raise InvalidIC(ins.quad, 'Unimplemented vard size: %s' % ins.quad[2]) for x in q: output.append('DEF%s %s' % (size, x)) return output
def _varx(ins)
Defines a memory space with a default CONSTANT expression 1st parameter is the var name 2nd parameter is the type-size (u8 or i8 for byte, u16 or i16 for word, etc) 3rd parameter is the list of expressions. All of them will be converted to the type required.
3.77379
3.436172
1.098254
output = [] output.append('%s:' % ins.quad[1]) q = eval(ins.quad[2]) for x in q: if x[0] == '#': # literal? size_t = 'W' if x[1] == '#' else 'B' output.append('DEF{0} {1}'.format(size_t, x.lstrip('#'))) continue # must be an hex number x = x.upper() assert RE_HEXA.match(x), 'expected an hex number, got "%s"' % x size_t = 'B' if len(x) <= 2 else 'W' if x[0] > '9': # Not a number? x = '0' + x output.append('DEF{0} {1}h'.format(size_t, x)) return output
def _vard(ins)
Defines a memory space with a default set of bytes/words in hexadecimal (starting with a number) or literals (starting with #). Numeric values with more than 2 digits represents a WORD (2 bytes) value. E.g. '01' => 0, '001' => 1, 0 bytes Literal values starts with # (1 byte) or ## (2 bytes) E.g. '#label + 1' => (label + 1) & 0xFF '##(label + 1)' => (label + 1) & 0xFFFF
4.577711
4.337409
1.055402
output = [] l = eval(ins.quad[3]) # List of bytes to push label = tmp_label() offset = int(ins.quad[1]) tmp = list(ins.quad) tmp[1] = label ins.quad = tmp AT_END.extend(_varx(ins)) output.append('push ix') output.append('pop hl') output.append('ld bc, %i' % -offset) output.append('add hl, bc') output.append('ex de, hl') output.append('ld hl, %s' % label) output.append('ld bc, %i' % (len(l) * YY_TYPES[ins.quad[2]])) output.append('ldir') return output
def _lvarx(ins)
Defines a local variable. 1st param is offset of the local variable. 2nd param is the type a list of bytes in hexadecimal.
7.28308
7.09257
1.02686
output = [] l = eval(ins.quad[2]) # List of bytes to push label = tmp_label() offset = int(ins.quad[1]) tmp = list(ins.quad) tmp[1] = label ins.quad = tmp AT_END.extend(_vard(ins)) output.append('push ix') output.append('pop hl') output.append('ld bc, %i' % -offset) output.append('add hl, bc') output.append('ex de, hl') output.append('ld hl, %s' % label) output.append('ld bc, %i' % len(l)) output.append('ldir') return output
def _lvard(ins)
Defines a local variable. 1st param is offset of the local variable. 2nd param is a list of bytes in hexadecimal.
7.122221
6.711096
1.06126
output = _8bit_oper(ins.quad[2]) output.extend(_16bit_oper(ins.quad[1])) output.append('ld b, h') output.append('ld c, l') output.append('out (c), a') return output
def _out(ins)
Translates OUT to asm.
8.366859
6.895968
1.213297
output = _16bit_oper(ins.quad[1]) output.append('ld b, h') output.append('ld c, l') output.append('in a, (c)') output.append('push af') return output
def _in(ins)
Translates IN to asm.
10.704836
8.793096
1.217414
output = _32bit_oper(ins.quad[2]) output.append('push de') output.append('push hl') return output
def _load32(ins)
Load a 32 bit value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value.
16.693865
17.734568
0.941318
output = _f16_oper(ins.quad[2]) output.append('push de') output.append('push hl') return output
def _loadf16(ins)
Load a 32 bit (16.16) fixed point value from a memory address If 2nd arg. start with '*', it is always treated as an indirect value.
14.887515
17.413553
0.854938
output = _float_oper(ins.quad[2]) output.extend(_fpush()) return output
def _loadf(ins)
Loads a floating point value from a memory address. If 2nd arg. start with '*', it is always treated as an indirect value.
40.171993
38.578815
1.041297
temporal, output = _str_oper(ins.quad[2], no_exaf=True) if not temporal: output.append('call __LOADSTR') REQUIRES.add('loadstr.asm') output.append('push hl') return output
def _loadstr(ins)
Loads a string value from a memory address.
29.247684
30.087662
0.972082
output = _8bit_oper(ins.quad[2]) op = ins.quad[1] indirect = op[0] == '*' if indirect: op = op[1:] immediate = op[0] == '#' if immediate: op = op[1:] if is_int(op) or op[0] == '_': if is_int(op): op = str(int(op) & 0xFFFF) if immediate: if indirect: output.append('ld (%s), a' % op) else: # ??? output.append('ld (%s), a' % op) elif indirect: output.append('ld hl, (%s)' % op) output.append('ld (hl), a') else: output.append('ld (%s), a' % op) else: if immediate: if indirect: # A label not starting with _ output.append('ld hl, (%s)' % op) output.append('ld (hl), a') else: output.append('ld (%s), a' % op) return output else: output.append('pop hl') if indirect: output.append('ld e, (hl)') output.append('inc hl') output.append('ld d, (hl)') output.append('ld (de), a') else: output.append('ld (hl), a') return output
def _store8(ins)
Stores 2nd operand content into address of 1st operand. store8 a, x => a = x Use '*' for indirect store on 1st operand.
3.121815
3.163098
0.986949
output = [] output = _16bit_oper(ins.quad[2]) try: value = ins.quad[1] indirect = False if value[0] == '*': indirect = True value = value[1:] value = int(value) & 0xFFFF if indirect: output.append('ex de, hl') output.append('ld hl, (%s)' % str(value)) output.append('ld (hl), e') output.append('inc hl') output.append('ld (hl), d') else: output.append('ld (%s), hl' % str(value)) except ValueError: if value[0] == '_': if indirect: output.append('ex de, hl') output.append('ld hl, (%s)' % str(value)) output.append('ld (hl), e') output.append('inc hl') output.append('ld (hl), d') else: output.append('ld (%s), hl' % str(value)) elif value[0] == '#': value = value[1:] if indirect: output.append('ex de, hl') output.append('ld hl, (%s)' % str(value)) output.append('ld (hl), e') output.append('inc hl') output.append('ld (hl), d') else: output.append('ld (%s), hl' % str(value)) else: output.append('ex de, hl') if indirect: output.append('pop hl') output.append('ld a, (hl)') output.append('inc hl') output.append('ld h, (hl)') output.append('ld l, a') else: output.append('pop hl') output.append('ld (hl), e') output.append('inc hl') output.append('ld (hl), d') return output
def _store16(ins)
Stores 2nd operand content into address of 1st operand. store16 a, x => *(&a) = x Use '*' for indirect store on 1st operand.
2.101378
2.117977
0.992163
op = ins.quad[1] indirect = op[0] == '*' if indirect: op = op[1:] immediate = op[0] == '#' # Might make no sense here? if immediate: op = op[1:] if is_int(op) or op[0] == '_' or immediate: output = _32bit_oper(ins.quad[2], preserveHL=indirect) if is_int(op): op = str(int(op) & 0xFFFF) if indirect: output.append('ld hl, (%s)' % op) output.append('call __STORE32') REQUIRES.add('store32.asm') return output output.append('ld (%s), hl' % op) output.append('ld (%s + 2), de' % op) return output output = _32bit_oper(ins.quad[2], preserveHL=True) output.append('pop hl') if indirect: output.append('call __ISTORE32') REQUIRES.add('store32.asm') return output output.append('call __STORE32') REQUIRES.add('store32.asm') return output
def _store32(ins)
Stores 2nd operand content into address of 1st operand. store16 a, x => *(&a) = x
4.144335
4.261772
0.972444
value = ins.quad[2] if is_float(value): val = float(ins.quad[2]) # Immediate? (de, hl) = f16(val) q = list(ins.quad) q[2] = (de << 16) | hl ins.quad = tuple(q) return _store32(ins)
def _storef16(ins)
Stores 2º operand content into address of 1st operand. store16 a, x => *(&a) = x
6.250197
6.54734
0.954616
output = _float_oper(ins.quad[2]) op = ins.quad[1] indirect = op[0] == '*' if indirect: op = op[1:] immediate = op[0] == '#' # Might make no sense here? if immediate: op = op[1:] if is_int(op) or op[0] == '_': if is_int(op): op = str(int(op) & 0xFFFF) if indirect: output.append('ld hl, (%s)' % op) else: output.append('ld hl, %s' % op) else: output.append('pop hl') if indirect: output.append('call __ISTOREF') REQUIRES.add('storef.asm') return output output.append('call __STOREF') REQUIRES.add('storef.asm') return output
def _storef(ins)
Stores a floating point value into a memory address.
5.192875
5.100468
1.018117
op1 = ins.quad[1] indirect = op1[0] == '*' if indirect: op1 = op1[1:] immediate = op1[0] == '#' if immediate and not indirect: raise InvalidIC('storestr does not allow immediate destination', ins.quad) if not indirect: op1 = '#' + op1 tmp1, tmp2, output = _str_oper(op1, ins.quad[2], no_exaf=True) if not tmp2: output.append('call __STORE_STR') REQUIRES.add('storestr.asm') else: output.append('call __STORE_STR2') REQUIRES.add('storestr2.asm') return output
def _storestr(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 '#'. Must prepend '#' (immediate sigil) to 1st operand, as we need the & address of the destination.
6.883989
7.047946
0.976737
# Signed and unsigned types are the same in the Z80 tA = ins.quad[2] # From TypeA tB = ins.quad[3] # To TypeB YY_TYPES[tA] # Type sizes xsB = sB = YY_TYPES[tB] # Type sizes output = [] if tA in ('u8', 'i8'): output.extend(_8bit_oper(ins.quad[4])) elif tA in ('u16', 'i16'): output.extend(_16bit_oper(ins.quad[4])) elif tA in ('u32', 'i32'): output.extend(_32bit_oper(ins.quad[4])) elif tA == 'f16': output.extend(_f16_oper(ins.quad[4])) elif tA == 'f': output.extend(_float_oper(ins.quad[4])) else: raise errors.GenericError( 'Internal error: invalid typecast from %s to %s' % (tA, tB)) if tB in ('u8', 'i8'): # It was a byte output.extend(to_byte(tA)) elif tB in ('u16', 'i16'): output.extend(to_word(tA)) elif tB in ('u32', 'i32'): output.extend(to_long(tA)) elif tB == 'f16': output.extend(to_fixed(tA)) elif tB == 'f': output.extend(to_float(tA)) xsB += sB % 2 # make it even (round up) if xsB > 4: output.extend(_fpush()) else: if xsB > 2: output.append('push de') # Fixed or 32 bit Integer if sB > 1: output.append('push hl') # 16 bit Integer else: output.append('push af') # 8 bit Integer return output
def _cast(ins)
Convert data from typeA to typeB (only numeric data types)
3.125281
3.032242
1.030683
value = ins.quad[1] if is_float(value): if float(value) == 0: return ['jp %s' % str(ins.quad[2])] # Always true else: return [] output = _float_oper(value) output.append('ld a, c') output.append('or l') output.append('or h') output.append('or e') output.append('or d') output.append('jp z, %s' % str(ins.quad[2])) return output
def _jzerof(ins)
Jumps if top of the stack (40bit, float) is 0 to arg(1)
4.881588
4.7147
1.035397
output = [] disposable = False # True if string must be freed from memory if ins.quad[1][0] == '_': # Variable? output.append('ld hl, (%s)' % ins.quad[1][0]) else: output.append('pop hl') output.append('push hl') # Saves it for later disposable = True output.append('call __STRLEN') if disposable: output.append('ex (sp), hl') output.append('call __MEM_FREE') output.append('pop hl') REQUIRES.add('alloc.asm') output.append('ld a, h') output.append('or l') output.append('jp z, %s' % str(ins.quad[2])) REQUIRES.add('strlen.asm') return output
def _jzerostr(ins)
Jumps if top of the stack contains a NULL pointer or its len is Zero
6.859957
6.89873
0.99438
value = ins.quad[1] if is_int(value): if int(value) != 0: return ['jp %s' % str(ins.quad[2])] # Always true else: return [] output = _16bit_oper(value) output.append('ld a, h') output.append('or l') output.append('jp nz, %s' % str(ins.quad[2])) return output
def _jnzero16(ins)
Jumps if top of the stack (16bit) is != 0 to arg(1)
5.76593
5.491062
1.050057
value = ins.quad[1] if is_int(value): if int(value) != 0: return ['jp %s' % str(ins.quad[2])] # Always true else: return [] output = _32bit_oper(value) output.append('ld a, h') output.append('or l') output.append('or e') output.append('or d') output.append('jp nz, %s' % str(ins.quad[2])) return output
def _jnzero32(ins)
Jumps if top of the stack (32bit) is !=0 to arg(1)
5.285254
5.099523
1.036421
value = ins.quad[1] if is_float(value): if float(value) != 0: return ['jp %s' % str(ins.quad[2])] # Always true else: return [] output = _f16_oper(value) output.append('ld a, h') output.append('or l') output.append('or e') output.append('or d') output.append('jp nz, %s' % str(ins.quad[2])) return output
def _jnzerof16(ins)
Jumps if top of the stack (32bit) is !=0 to arg(1) Fixed Point (16.16 bit) values.
5.31701
5.483327
0.969669
output = [] value = ins.quad[1] if not is_int(value): output = _8bit_oper(value) output.append('jp %s' % str(ins.quad[2])) return output
def _jgezerou8(ins)
Jumps if top of the stack (8bit) is >= 0 to arg(1) Always TRUE for unsigned
9.334437
10.6086
0.879893
value = ins.quad[1] if is_int(value): if int(value) >= 0: return ['jp %s' % str(ins.quad[2])] # Always true else: return [] output = _8bit_oper(value) output.append('add a, a') # Puts sign into carry output.append('jp nc, %s' % str(ins.quad[2])) return output
def _jgezeroi8(ins)
Jumps if top of the stack (8bit) is >= 0 to arg(1)
6.999773
6.767516
1.034319
output = [] value = ins.quad[1] if not is_int(value): output = _16bit_oper(value) output.append('jp %s' % str(ins.quad[2])) return output
def _jgezerou16(ins)
Jumps if top of the stack (16bit) is >= 0 to arg(1) Always TRUE for unsigned
8.610281
9.883459
0.871181
output = [] value = ins.quad[1] if not is_int(value): output = _32bit_oper(value) output.append('jp %s' % str(ins.quad[2])) return output
def _jgezerou32(ins)
Jumps if top of the stack (23bit) is >= 0 to arg(1) Always TRUE for unsigned
8.746615
10.224223
0.85548
output = _8bit_oper(ins.quad[1]) output.append('#pragma opt require a') output.append('jp %s' % str(ins.quad[2])) return output
def _ret8(ins)
Returns from a procedure / function an 8bits value
19.594597
17.855316
1.09741
output = _16bit_oper(ins.quad[1]) output.append('#pragma opt require hl') output.append('jp %s' % str(ins.quad[2])) return output
def _ret16(ins)
Returns from a procedure / function a 16bits value
21.248531
19.914801
1.066972
output = _32bit_oper(ins.quad[1]) output.append('#pragma opt require hl,de') output.append('jp %s' % str(ins.quad[2])) return output
def _ret32(ins)
Returns from a procedure / function a 32bits value (even Fixed point)
23.498899
21.702038
1.082797
output = _f16_oper(ins.quad[1]) output.append('#pragma opt require hl,de') output.append('jp %s' % str(ins.quad[2])) return output
def _retf16(ins)
Returns from a procedure / function a Fixed Point (32bits) value
21.236443
21.060305
1.008364
output = _float_oper(ins.quad[1]) output.append('#pragma opt require a,bc,de') output.append('jp %s' % str(ins.quad[2])) return output
def _retf(ins)
Returns from a procedure / function a Floating Point (40bits) value
21.307287
20.985949
1.015312
tmp, output = _str_oper(ins.quad[1], no_exaf=True) if not tmp: output.append('call __LOADSTR') REQUIRES.add('loadstr.asm') output.append('#pragma opt require hl') output.append('jp %s' % str(ins.quad[2])) return output
def _retstr(ins)
Returns from a procedure / function a string pointer (16bits) value
22.386545
23.910389
0.936269
output = [] output.append('call %s' % str(ins.quad[1])) try: val = int(ins.quad[2]) if val == 1: output.append('push af') # Byte else: if val > 4: output.extend(_fpush()) else: if val > 2: output.append('push de') if val > 1: output.append('push hl') except ValueError: pass return output
def _call(ins)
Calls a function XXXX (or address XXXX) 2nd parameter contains size of the returning result if any, and will be pushed onto the stack.
4.978584
5.128573
0.970754
global FLAG_use_function_exit output = [] if ins.quad[1] == '__fastcall__': output.append('ret') return output nbytes = int(ins.quad[1]) # Number of bytes to pop (params size) if nbytes == 0: output.append('ld sp, ix') output.append('pop ix') output.append('ret') return output if nbytes == 1: output.append('ld sp, ix') output.append('pop ix') output.append('inc sp') # "Pops" 1 byte output.append('ret') return output if nbytes <= 11: # Number of bytes it worth the hassle to "pop" off the stack output.append('ld sp, ix') output.append('pop ix') output.append('exx') output.append('pop hl') for i in range((nbytes >> 1) - 1): output.append('pop bc') # Removes (n * 2 - 2) bytes form the stack if nbytes & 1: # Odd? output.append('inc sp') # "Pops" 1 byte (This should never happens, since params are always even-sized) output.append('ex (sp), hl') # Place back return address output.append('exx') output.append('ret') return output if not FLAG_use_function_exit: FLAG_use_function_exit = True # Use standard exit output.append('exx') output.append('ld hl, %i' % nbytes) output.append('__EXIT_FUNCTION:') output.append('ld sp, ix') output.append('pop ix') output.append('pop de') output.append('add hl, sp') output.append('ld sp, hl') output.append('push de') output.append('exx') output.append('ret') else: output.append('exx') output.append('ld hl, %i' % nbytes) output.append('jp __EXIT_FUNCTION') return output
def _leave(ins)
Return from a function popping N bytes from the stack Use '__fastcall__' as 1st parameter, to just return
3.947549
3.766023
1.048201
output = [] if ins.quad[1] == '__fastcall__': return output output.append('push ix') output.append('ld ix, 0') output.append('add ix, sp') size_bytes = int(ins.quad[1]) if size_bytes != 0: if size_bytes < 7: output.append('ld hl, 0') output.extend(['push hl'] * (size_bytes >> 1)) if size_bytes % 2: # odd? output.append('push hl') output.append('inc sp') else: output.append('ld hl, -%i' % size_bytes) # "Pushes nn bytes" output.append('add hl, sp') output.append('ld sp, hl') output.append('ld (hl), 0') output.append('ld bc, %i' % (size_bytes - 1)) output.append('ld d, h') output.append('ld e, l') output.append('inc de') output.append('ldir') # Clear with ZEROs return output
def _enter(ins)
Enter function sequence for doing a function start ins.quad[1] contains size (in bytes) of local variables Use '__fastcall__' as 1st parameter to prepare a fastcall function (no local variables).
4.817005
4.311672
1.117201
output = _32bit_oper(ins.quad[1]) output.append('push de') output.append('push hl') return output
def _param32(ins)
Pushes 32bit param into the stack
17.639076
15.304951
1.152508
output = _f16_oper(ins.quad[1]) output.append('push de') output.append('push hl') return output
def _paramf16(ins)
Pushes 32bit fixed point param into the stack
16.323719
16.068632
1.015875
output = _float_oper(ins.quad[1]) output.extend(_fpush()) return output
def _paramf(ins)
Pushes 40bit (float) param into the stack
40.376278
28.93227
1.395545
(tmp, output) = _str_oper(ins.quad[1]) output.pop() # Remove a register flag (useless here) tmp = ins.quad[1][0] in ('#', '_') # Determine if the string must be duplicated if tmp: output.append('call __LOADSTR') # Must be duplicated REQUIRES.add('loadstr.asm') output.append('push hl') return output
def _paramstr(ins)
Pushes an 16 bit unsigned value, which points to a string. For indirect values, it will push the pointer to the pointer :-)
19.083111
18.729595
1.018875
output = _16bit_oper(ins.quad[3]) output.append('ld b, h') output.append('ld c, l') output.extend(_16bit_oper(ins.quad[1], ins.quad[2], reversed=True)) output.append('ldir') # *** return output
def _memcopy(ins)
Copies a block of memory from param 2 addr to param 1 addr.
9.427575
10.74511
0.877383
tmp = [x.strip(' \t\r\n') for x in ins.quad[1].split('\n')] # Split lines i = 0 while i < len(tmp): if not tmp[i] or tmp[i][0] == ';': # a comment or empty string? tmp.pop(i) continue if tmp[i][0] == '#': # A preprocessor directive i += 1 continue match = RE_LABEL.match(tmp[i]) if not match: tmp[i] = '\t' + tmp[i] i += 1 continue if len(tmp[i][-1]) == ':': i += 1 continue # This is already a single label tmp[i] = tmp[i][match.end() + 1:].strip(' \n') tmp.insert(i, match.group()) i += 1 output = [] if not tmp: return output ASMLABEL = new_ASMID() ASMS[ASMLABEL] = tmp output.append('#line %s' % ins.quad[2]) output.append(ASMLABEL) output.append('#line %i' % (int(ins.quad[2]) + len(tmp))) return output
def _inline(ins)
Inline code
3.861979
3.815592
1.012157
if not OPTIONS.strictBool.value: return [] REQUIRES.add('strictbool.asm') result = [] result.append('pop af') result.append('call __NORMALIZE_BOOLEAN') result.append('push af') return result
def convertToBool()
Convert a byte value to boolean (0 or 1) if the global flag strictBool is True
17.274921
16.754572
1.031057