コード例 #1
0
ファイル: array.py プロジェクト: mmmulani/cs444
def _array_init_loop(is_primitive):
  ''' Code that loops through array elements to initialize them.
  This code does not save/restore registers, it should only be called from
  create_array() '''

  # Code that will create the default value for a single array element:
  default_value_code = asm_object.create_default_value(is_primitive, False)

  loop_start, loop_end = CodeGenManager.get_labels(
    'array_init_loop_start', 'array_init_loop_end')

  # eax gets set to the an element'ss default value
  # ebx stores the length of the array (number of elements)
  # ecx is the loop counter
  # edx is a pointer to the start of the array
  return [
    'mov ecx, 0  ; ecx is counter for which array index we\'re at',
    '{0}:'.format(loop_start),
    'cmp ecx, ebx  ; loop condition (check if done all array elements)',
    'je {0}'.format(loop_end),
    '; init current array elem to default value:',
    'push ebx',
    default_value_code,
    'pop ebx',
    'mov [edx + 12 + 4*ecx], eax',
    'add ecx, 1  ; increment loop counter',
    'jmp {0}'.format(loop_start),
    '{0}:'.format(loop_end),
  ]
コード例 #2
0
ファイル: ast_class.py プロジェクト: mmmulani/cs444
  def c_gen_code_create_instance(self):
    '''Creates an instance of this class in memory and does all prep work so
    that the constructor can be called. Precisely, does the following:

    1. Allocate memory for the object.
    2. Sets a pointer to the CIT on the instance.
    3. Initializes all fields to their default value.

    The following should be done by the ClassInstanceCreation:
    1. Call the parent constructor.
    2. Initialize fields declared on the class.
    3. Run constructor body.'''

    import code_gen.asm.common as common
    import code_gen.asm.object as object_

    field_init_code = []
    for f in self.get_all_fields():
      type_node = f.type_node
      field_init_code.extend([
        'push eax ; save |this|',
        object_.create_default_value(type_node.is_primitive,
            type_node.is_array),
        'mov ebx, eax ; store result in ebx',
        'pop eax ; restore |this|',
        common.save_instance_field('eax', f, 'ebx'),
      ])

    return [
      'global {0}'.format(self.c_create_object_function_label),
      '{0}:'.format(self.c_create_object_function_label),
      common.malloc(self.c_object_size),
      # Class info table
      'mov dword [eax], {0}'.format(self.c_cit_label),
      field_init_code,
      'ret',
    ]