Beispiel #1
0
def divide_int():
  '''Divides one integer by another and returns the address of a new integer
  equal to the result.

  2 Params:
    1. The address of an integer (left operand - dividend)
    2. The address of another integer (right operand - divisor)'''
  N_PARAMS = 2

  div_not_zero = CodeGenManager.get_label('div_not_zero')

  return [
    '_divide_int:',
    common.function_prologue(),
    '; get the value for the left operand and put it in eax',
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    '; get the value for the right operand and put in in ebx',
    common.get_param('ebx', 1, N_PARAMS),
    common.unwrap_primitive('ebx', 'ebx'),
    '; check for division by zero:',
    'cmp ebx, 0',
    'jne {0}'.format(div_not_zero),
    'call __exception',
    '{0}:'.format(div_not_zero),
    common.fill_high_order_bit('eax', 'edx'),
    'idiv ebx  ; sets eax to edx:eax/ebx',
    '; create an int with the result',
    'push eax  ; the result of div has to be in eax',
    'call _create_int',
    'pop ebx ; pop param',
    '; eax is an integer object with the old value of eax',
    common.function_epilogue()
  ]
Beispiel #2
0
def add_int():
  '''Adds two integers together and returns the address of a new integer with
  the result.

  2 Params:
    1. The address of an integer (left operand)
    2. The address of another integer (right operand)'''
  N_PARAMS = 2

  return [
      '_add_int:',
      common.function_prologue(),
      '; get the value for the left operand and put it in ecx',
      common.get_param('ecx', 0, N_PARAMS),
      common.unwrap_primitive('ecx', 'ecx'),
      '; get the value for the right operand and put in in edx',
      common.get_param('edx', 1, N_PARAMS),
      common.unwrap_primitive('edx', 'edx'),
      'add ecx, edx',
      '; create an int with the result',
      'push ecx',
      'call _create_int',
      'pop ebx ; pop param',
      '; eax is an integer object with the value of ecx',
      common.function_epilogue()
  ]
Beispiel #3
0
def not_equals_ref():
  '''Returns a boolean if both references are not equal.

  2 Params:
    1. Address of a reference (left operand)
    2. Address of a reference (right operand)
  '''
  N_PARAMS = 2

  return [
    '_not_equals_ref:',
    common.function_prologue(),
    common.get_param('eax', 0, N_PARAMS),
    common.get_param('ebx', 1, N_PARAMS),
    'cmp eax, ebx',
    'je _not_equals_ref_same',
    'push 1 ; only run if not the same',
    'jmp _not_equals_ref_done',
    '_not_equals_ref_same:',
    'push 0 ; only run if the same',
    '_not_equals_ref_done:',
    'call _create_boolean',
    'pop ebx ; pop to garbage',
    common.function_epilogue(),
  ]
Beispiel #4
0
def multiply_int():
  '''Multiplies two integers and returns the address of the result

  2 Params:
    1. The address of an integer (left operand)
    2. The address of an integer (right operand)
  '''
  N_PARAMS = 2

  return [
    '_mult_int:',
    common.function_prologue(),
    '; Get the left param and put it in eax',
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    '; Get the right param and put it in ebx',
    common.get_param('ebx', 1, N_PARAMS),
    common.unwrap_primitive('ebx', 'ebx'),
    'imul eax, ebx  ; Multiply the two params',
    '; Create an int with the result.',
    'push eax  ; Push the int as a param',
    'call _create_int',
    'pop ebx ; Pop off param',
    common.function_epilogue()
  ]
Beispiel #5
0
def equals_prim():
  '''Returns a boolean if both primitives are equal.

  2 Params:
    1. Address of a primitive (left operand)
    2. Address of a primitive (right operand)
  '''
  N_PARAMS = 2

  return [
    '_equals_prim:',
    common.function_prologue(),
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('ebx', 'eax'),
    common.get_param('eax', 1, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    'cmp eax, ebx',
    'je _equals_prim_same',
    'push 0 ; only run if not the same',
    'jmp _equals_prim_done',
    '_equals_prim_same:',
    'push 1 ; only run if the same',
    '_equals_prim_done:',
    'call _create_boolean',
    'pop ebx ; pop to garbage',
    common.function_epilogue(),
  ]
Beispiel #6
0
def mod_int():
  '''Computes the modulus of two integers and returns the address of the result

  2 Params:
    1. The address of an integer (dividend)
    2. The address of an integer (dividend)
  '''
  N_PARAMS = 2

  return [
    '_mod_int:',
    common.function_prologue(),
    '; get the value for the left operand and put it in eax',
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    '; get the value for the right operand and put in in ebx',
    common.get_param('ebx', 1, N_PARAMS),
    common.unwrap_primitive('ebx', 'ebx'),
    common.fill_high_order_bit('eax', 'edx'),
    'idiv ebx  ; sets edx to edx:eax mod ebx',
    '; create an int with the result',
    'push edx ; the result of mod is placed in ebx',
    'call _create_int',
    'pop ebx ; pop param',
    '; eax is an integer object with the old value of edx',
    common.function_epilogue()
  ]
Beispiel #7
0
def create_int():
  '''Allocates space in memory for an integer (32-bits).
  The value at the memory address will be set to the parameter passed in.

  1 Param:
    The value of the integer'''
  N_PARAMS = 1

  return [
      '_create_int:',
      common.function_prologue(),
      common.malloc(4),
      common.get_param('ebx', 0, N_PARAMS),
      'mov dword [eax], ebx',
      common.function_epilogue()
  ]
Beispiel #8
0
def negate_bool():
  '''Negates a bool and returns the address of a new integer.

  1 Param:
    1. The address of the integer to negate.'''
  N_PARAMS = 1

  return [
      '_negate_bool:',
      common.function_prologue(),
      common.get_param('ebx', 0, N_PARAMS),
      common.unwrap_primitive('ebx', 'ebx'),
      'mov eax, 1',
      'xor eax, ebx',
      'push eax',
      'call _create_boolean',
      'pop ebx ; pop param',
      common.function_epilogue(),
  ]
Beispiel #9
0
 def asm_op():
   return [
     '_{0}:'.format(name),
     common.function_prologue(),
     common.get_param('eax', 0, N_PARAMS),
     common.unwrap_primitive('eax', 'eax'),
     common.get_param('ebx', 1, N_PARAMS),
     common.unwrap_primitive('ebx', 'ebx'),
     'cmp eax, ebx',
     '{0} _{1}_true'.format(x86_op, name),
     'push 0 ; only run if fail comparison',
     'jmp _{0}_done'.format(name),
     '_{0}_true:'.format(name),
     'push 1 ; only run if pass comparison',
     '_{0}_done:'.format(name),
     'call _create_boolean',
     'pop ebx ; pop to garbage',
     common.function_epilogue(),
   ]
Beispiel #10
0
def sub_int():
  '''Subtracts two integers (right from left) and returns the address of a new
  integer with the result.

  2 Params:
    1. The address of an integer (left operand)
    2. The address of an integer (right operand)'''
  N_PARAMS = 2

  return [
      '_sub_int:',
      common.function_prologue(),
      common.get_param('ecx', 0, N_PARAMS),
      common.unwrap_primitive('ecx', 'ecx'),
      common.get_param('edx', 1, N_PARAMS),
      common.unwrap_primitive('edx', 'edx'),
      'sub ecx, edx',
      '; create an int with the result',
      'push ecx',
      'call _create_int',
      'pop ebx ; pop param',
      common.function_epilogue()
  ]
Beispiel #11
0
def eager_or():
  '''Computes the OR of two booleans and returns the address of the result.

  2 Params:
    1. Address of a boolean (left operand)
    2. Address of a boolean (right operand)
  '''
  N_PARAMS = 2

  return [
    '_eager_or:',
    common.function_prologue(),
    common.get_param('eax', 0, N_PARAMS),
    common.unwrap_primitive('ebx', 'eax'),
    common.get_param('eax', 1, N_PARAMS),
    common.unwrap_primitive('eax', 'eax'),
    'or eax, ebx',
    'push eax',
    'call _create_boolean',
    'pop ebx ; pop param to garbage',
    '; result is stored in eax',
    common.function_epilogue()
  ]
Beispiel #12
0
def create_boolean():
  '''Allocates space in memory for a boolean (stores it in 32-bits).
  The value at the memory address will be set to 0 or 1 based on the parameter
  passed in:
    0: if the parameter is 0
    1: otherwise

  1 Param:
    Integer value that will be converted to boolean based on above.'''
  N_PARAMS = 1

  return [
      '_create_boolean:',
      common.function_prologue(),
      common.malloc(4),
      common.get_param('ebx', 0, N_PARAMS),
      'mov ecx, 0',
      'cmp ebx, ecx',
      'je _create_boolean_done_compare',
      'mov ebx, 1',
      '_create_boolean_done_compare:',
      'mov dword [eax], ebx',
      common.function_epilogue()
  ]