def type_check_LD(context, destination, source): destination_size = None ''' Lookup destination operand ''' if isinstance(destination, O.Field): if not is_special_field(destination.field): if destination.field in context.header: destination_size = context.header[destination.field] else: raise TypeError("destination field (%s) is not present in the header." % str(destination.field)) else: raise TypeError("destination field (%s) is a special field." % str(destination.field)) else: raise TypeError("invalid %s of destination (%s). Should be %s." % (type(destination), destination, O.Field)) source_size = None ''' Lookup source operand ''' if isinstance(source, O.Value): source_size = source.value.size elif isinstance(source, O.Field): if not is_special_field(source.field): if source.field in context.header: source_size = context.header[source.field] else: raise TypeError("source field (%s) is not present in the header." % str(source.field)) else: raise TypeError("source field (%s) is a special field." % str(source.field)) elif isinstance(source, O.Location): offset = source.location.offset if isinstance(offset, O.Value): pass elif isinstance(offset, O.Field): if not is_special_field(offset.field): if offset.field in context.header: pass else: raise TypeError("source location's offset field (%s) is not present in the header." % str(offset.field)) else: raise TypeError("source location's offset field (%s) is a special field." % str(offset.field)) else: raise TypeError("invalid %s of source location's offset (%s). Should be either %s or %s." % (type(offset), offset, O.Value, O.Field)) source_size = destination_size else: raise TypeError("invalid %s of source (%s). Should be %s, %s, or %s." % (type(source), source, O.Value, O.Field, O.Location)) ''' Compare sizes ''' if source_size <= destination_size: pass else: raise TypeError("source (%s) size (%s) should be less than destination (%s) size (%s)." % (source, source_size, destination, destination_size)) context.labels = Labels(syntax.Label(''))
def type_check_BR(context, left_source, operator, right_source, label): pass ''' Lookup left_source operand ''' if isinstance(left_source, O.Value): pass elif isinstance(left_source, O.Field): if not is_special_field(left_source.field): if left_source.field in context.header: pass else: raise TypeError( "left source field (%s) is not present in the header." % str(left_source.field)) else: raise TypeError("left source field (%s) is a special field." % str(left_source.field)) else: raise TypeError( "invalid %s of left source (%s). Should be either %s or %s." % (type(left_source), left_source, O.Value, O.Field)) ''' Lookup right_source operand ''' if isinstance(right_source, O.Value): pass elif isinstance(right_source, O.Field): if not is_special_field(right_source.field): if right_source.field in context.header: pass else: raise TypeError( "right source field (%s) is not present in the header." % str(right_source.field)) else: raise TypeError("right source field (%s) is a special field." % str(right_source.field)) else: raise TypeError( "invalid %s of right source (%s). Should be either %s or %s." % (type(right_source), right_source, O.Value, O.Field)) if isinstance(operator, Op.ComparisonOperator): pass else: raise TypeError("invalid %s of operator (%s). Should be %s." % (type(operator), operator, Op.ComparisonOperator)) ''' Update program counter ''' if not isinstance(label, syntax.Label): raise TypeError("invalid %s of label (%s). Should be %s." % (type(label), label, syntax.Label)) # TODO: check this using the forwards and backwards label check # if label in labels: # pass # else: # raise TypeError("TypeError(%s): label (%s) doesn't exist" % ("BR", str(label))) context.labels = Labels(label, syntax.Label(''))
def type_check_BR(context, left_source, operator, right_source, label): pass ''' Lookup left_source operand ''' if isinstance(left_source, O.Value): pass elif isinstance(left_source, O.Field): if not is_special_field(left_source.field): if left_source.field in context.header: pass else: raise TypeError( "left source field (%s) is not present in the header." % str(left_source.field)) else: raise TypeError("left source field (%s) is a special field." % str(left_source.field)) else: raise TypeError("invalid %s of left source (%s). Should be either %s or %s." % (type(left_source), left_source, O.Value, O.Field)) ''' Lookup right_source operand ''' if isinstance(right_source, O.Value): pass elif isinstance(right_source, O.Field): if not is_special_field(right_source.field): if right_source.field in context.header: pass else: raise TypeError( "right source field (%s) is not present in the header." % str(right_source.field)) else: raise TypeError("right source field (%s) is a special field." % str(right_source.field)) else: raise TypeError("invalid %s of right source (%s). Should be either %s or %s." % (type(right_source), right_source, O.Value, O.Field)) if isinstance(operator, Op.ComparisonOperator): pass else: raise TypeError("invalid %s of operator (%s). Should be %s." % (type(operator), operator, Op.ComparisonOperator)) ''' Update program counter ''' if not isinstance(label, syntax.Label): raise TypeError("invalid %s of label (%s). Should be %s." % (type(label), label, syntax.Label)) # TODO: check this using the forwards and backwards label check # if label in labels: # pass # else: # raise TypeError("TypeError(%s): label (%s) doesn't exist" % ("BR", str(label))) context.labels = Labels(label, syntax.Label(''))
def type_check_PUSH(context, location, source): source_size = None ''' Lookup source operand ''' if isinstance(source, O.Value): source_size = source.value.size elif isinstance(source, O.Field): if not is_special_field(source.field): if source.field in context.header: source_size = context.header[source.field] else: raise TypeError( "source field (%s) is not present in the header." % str(source.field)) else: raise TypeError("source field (%s) is a special field." % str(source.field)) else: raise TypeError( "invalid %s of source (%s). Should be either %s or %s." % (type(source), source, O.Value, O.Field)) ''' lookup location operand ''' location_size = None if isinstance(location, O.Location): offset = location.location.offset if isinstance(offset, O.Value): pass elif isinstance(offset, O.Field): if not is_special_field(offset.field): if offset.field in context.header: pass else: raise TypeError( "location's offset field (%s) is not present in the header." % str(offset.field)) else: raise TypeError( "location's offset field (%s) is a special field." % str(offset.field)) else: raise TypeError( "invalid %s of location's offset (%s). Should be either %s or %s." % (type(offset), offset, O.Value, O.Field)) location_size = source_size else: raise TypeError("invalid %s of location (%s). Should be %s." % (type(location), location, O.Location)) ''' Compare sizes ''' if source_size <= location_size: pass else: raise TypeError( "source (%s) size (%s) should be less than location (%s) size (%s)." % (source, source_size, location, location_size)) context.labels = Labels(syntax.Label(''))
def type_check_POP(context, destination, location): destination_size = None ''' Lookup destination operand ''' if isinstance(destination, O.Field): if not is_special_field(destination.field): if destination.field in context.header: destination_size = context.header[destination.field] else: raise TypeError( "destination field (%s) is not present in the header." % str(destination.field)) else: raise TypeError("destination field (%s) is a special field." % str(destination.field)) else: raise TypeError("invalid %s of destination (%s). Should be %s." % (type(destination), destination, O.Field)) location_size = None ''' Lookup source operand ''' if isinstance(location, O.Location): offset = location.location.offset if isinstance(offset, O.Value): pass elif isinstance(offset, O.Field): if not is_special_field(offset.field): if offset.field in context.header: pass else: raise TypeError( "location's offset field (%s) is not present in the header." % str(offset.field)) else: raise TypeError( "location's offset field (%s) is a special field." % str(offset.field)) else: raise TypeError( "invalid %s of location's offset (%s). Should be either %s or %s." % (type(offset), offset, O.Value, O.Location)) location_size = destination_size else: raise TypeError("invalid %s of location (%s). Should be %s." % (type(location), location, O.Location)) ''' Compare sizes ''' if location_size <= destination_size: pass else: raise TypeError( "location (%s) size (%s) should be less than destination (%s) size (%s)." % (location, location_size, destination, destination_size)) context.labels = Labels(syntax.Label(''))
def type_check_OP(context, destination, left_source, operator, right_source): left_size = None ''' Lookup left_source operand ''' if isinstance(left_source, O.Value): left_size = left_source.value.size elif isinstance(left_source, O.Field): if not is_special_field(left_source.field): if left_source.field in context.header: left_size = context.header[left_source.field] else: raise TypeError( "left source field (%s) is not present in the header." % str(left_source.field)) else: raise TypeError("left source field (%s) is a special field." % str(left_source.field)) else: raise TypeError("invalid %s of left source (%s). Should be %s or %s." % (type(left_source), left_source, O.Value, O.Field)) ''' Lookup right_source operand ''' right_size = None if isinstance(right_source, O.Value): right_size = right_source.value.size elif isinstance(right_source, O.Field): if not is_special_field(right_source.field): if right_source.field in context.header: right_size = context.header[right_source.field] else: raise TypeError( "right source field (%s) is not present in the header." % str(right_source.field)) else: raise TypeError("right source field (%s) is a special field." % str(right_source.field)) else: raise TypeError( "invalid %s of right source (%s). Should be %s or %s." % (type(right_source), right_source, O.Value, O.Field)) ''' Operate on the values ''' if isinstance(operator, Op.ArithmeticBitwiseOperator): pass else: raise TypeError( "invalid %s of operator (%s). Should be %s." % (type(operator), operator, Op.ArithmeticBitwiseOperator)) ''' Lookup destination operand ''' destination_size = None if isinstance(destination, O.Field): if not is_special_field(destination.field): if destination.field in context.header: destination_size = context.header[destination.field] else: raise TypeError( "destination field (%s) is not present in the header." % str(destination.field)) else: raise TypeError("destination field (%s) is a special field." % str(destination.field)) else: raise TypeError("invalid %s of destination (%s). Should be %s." % (type(destination), destination, O.Field)) # TODO: make proper comparisons using the operator types (i.e., mul operator requires double desintation size) ''' Compare sizes ''' if left_size <= destination_size: pass else: raise TypeError( "left source (%s) size (%s) should be less than destination (%s) size (%s)." % (left_source, left_size, destination, destination_size)) if right_size <= destination_size: pass else: raise TypeError( "right source (%s) size (%s) should be less than destination (%s) size (%s)." % (right_source, right_size, destination, destination_size)) context.labels = Labels(syntax.Label(''))
def type_check_OP(context, destination, left_source, operator, right_source): left_size = None ''' Lookup left_source operand ''' if isinstance(left_source, O.Value): left_size = left_source.value.size elif isinstance(left_source, O.Field): if not is_special_field(left_source.field): if left_source.field in context.header: left_size = context.header[left_source.field] else: raise TypeError("left source field (%s) is not present in the header." % str(left_source.field)) else: raise TypeError("left source field (%s) is a special field." % str(left_source.field)) else: raise TypeError("invalid %s of left source (%s). Should be %s or %s." % (type(left_source), left_source, O.Value, O.Field)) ''' Lookup right_source operand ''' right_size = None if isinstance(right_source, O.Value): right_size = right_source.value.size elif isinstance(right_source, O.Field): if not is_special_field(right_source.field): if right_source.field in context.header: right_size = context.header[right_source.field] else: raise TypeError("right source field (%s) is not present in the header." % str(right_source.field)) else: raise TypeError("right source field (%s) is a special field." % str(right_source.field)) else: raise TypeError("invalid %s of right source (%s). Should be %s or %s." % (type(right_source), right_source, O.Value, O.Field)) ''' Operate on the values ''' if isinstance(operator, Op.ArithmeticBitwiseOperator): pass else: raise TypeError("invalid %s of operator (%s). Should be %s." % (type(operator), operator, Op.ArithmeticBitwiseOperator)) ''' Lookup destination operand ''' destination_size = None if isinstance(destination, O.Field): if not is_special_field(destination.field): if destination.field in context.header: destination_size = context.header[destination.field] else: raise TypeError("destination field (%s) is not present in the header." % str(destination.field)) else: raise TypeError("destination field (%s) is a special field." % str(destination.field)) else: raise TypeError("invalid %s of destination (%s). Should be %s." % (type(destination), destination, O.Field)) # TODO: make proper comparisons using the operator types (i.e., mul operator requires double desintation size) ''' Compare sizes ''' if left_size <= destination_size: pass else: raise TypeError("left source (%s) size (%s) should be less than destination (%s) size (%s)." % (left_source, left_size, destination, destination_size)) if right_size <= destination_size: pass else: raise TypeError("right source (%s) size (%s) should be less than destination (%s) size (%s)." % (right_source, right_size, destination, destination_size)) context.labels = Labels(syntax.Label(''))