# HG changeset patch # User Paul Boddie # Date 1488193035 -3600 # Node ID 3bba67784f79851401e04ed7742f3ee64b0e68b2 # Parent 5a69a5a45afbb7391e7be1a4a8085cd684435226 Encode attribute and parameter positions and codes using the provided macros. diff -r 5a69a5a45afb -r 3bba67784f79 encoders.py --- a/encoders.py Mon Feb 27 00:55:52 2017 +0100 +++ b/encoders.py Mon Feb 27 11:57:15 2017 +0100 @@ -277,21 +277,21 @@ if op in attribute_ops: arg = a[1] - a[1] = encode_symbol("pos", arg) + a[1] = encode_pos(arg) # Convert attribute name arguments to position and code symbols. elif op in checked_ops: arg = a[1] - a[1] = encode_symbol("pos", arg) - a.insert(2, encode_symbol("code", arg)) + a[1] = encode_pos(arg) + a.insert(2, encode_code(arg)) # Convert type name arguments to position and code symbols. elif op in typename_ops: arg = encode_type_attribute(args[1]) - a[1] = encode_symbol("pos", arg) - a.insert(2, encode_symbol("code", arg)) + a[1] = encode_pos(arg) + a.insert(2, encode_code(arg)) # Obtain addresses of type arguments. @@ -509,6 +509,30 @@ all_encoded_paths[encoded] = path return encoded +def encode_code(name): + + "Encode 'name' as an attribute code indicator." + + return "__ATTRCODE(%s)" % encode_path(name) + +def encode_pcode(name): + + "Encode 'name' as an parameter code indicator." + + return "__PARAMCODE(%s)" % encode_path(name) + +def encode_pos(name): + + "Encode 'name' as an attribute position indicator." + + return "__ATTRPOS(%s)" % encode_path(name) + +def encode_ppos(name): + + "Encode 'name' as an parameter position indicator." + + return "__PARAMPOS(%s)" % encode_path(name) + def encode_predefined_reference(path): "Encode a reference to a predefined constant value for 'path'." diff -r 5a69a5a45afb -r 3bba67784f79 generator.py --- a/generator.py Mon Feb 27 00:55:52 2017 +0100 +++ b/generator.py Mon Feb 27 11:57:15 2017 +0100 @@ -20,13 +20,14 @@ """ from common import CommonOutput, copy -from encoders import encode_function_pointer, \ +from encoders import encode_code, \ + encode_function_pointer, \ encode_instantiator_pointer, \ encode_literal_constant, encode_literal_constant_member, \ encode_literal_constant_size, encode_literal_constant_value, \ encode_literal_data_initialiser, \ encode_literal_instantiator, encode_literal_reference, \ - encode_path, \ + encode_path, encode_pcode, encode_pos, encode_ppos, \ encode_predefined_reference, encode_size, \ encode_symbol, encode_tablename, \ encode_type_attribute, decode_type_attribute, \ @@ -154,6 +155,8 @@ print >>f_consts, """\ #ifndef __PROGCONSTS_H__ #define __PROGCONSTS_H__ + +#include "types.h" """ print >>f_decls, """\ #ifndef __PROGTYPES_H__ @@ -379,11 +382,15 @@ # Generate parameter codes. - self.write_code_constants(f_consts, self.optimiser.all_paramnames, self.optimiser.arg_locations, "pcode", "ppos") + self.write_code_constants(f_consts, self.optimiser.all_paramnames, + self.optimiser.arg_locations, + "pcode", "ppos", encode_pcode, encode_ppos) # Generate attribute codes. - self.write_code_constants(f_consts, self.optimiser.all_attrnames, self.optimiser.locations, "code", "pos") + self.write_code_constants(f_consts, self.optimiser.all_attrnames, + self.optimiser.locations, + "code", "pos", encode_code, encode_pos) # Output more boilerplate. @@ -403,8 +410,8 @@ encode_path(self.function_type), encode_size("", self.function_type), encode_path(self.type_type), - encode_symbol("pos", encode_type_attribute(self.type_type)), - encode_symbol("code", encode_type_attribute(self.type_type)), + encode_pos(encode_type_attribute(self.type_type)), + encode_code(encode_type_attribute(self.type_type)), ) print >>f_signatures, """\ @@ -620,7 +627,8 @@ f_consts.write(" %s = %d" % (encode_size(size_prefix, path), size + padding)) print >>f_consts, "\n };" - def write_code_constants(self, f_consts, attrnames, locations, code_prefix, pos_prefix): + def write_code_constants(self, f_consts, attrnames, locations, code_prefix, + pos_prefix, code_encoder, pos_encoder): """ Write code constants to 'f_consts' for the given 'attrnames' and @@ -634,7 +642,7 @@ print >>f_consts, "," else: first = False - f_consts.write(" %s = %d" % (encode_symbol(code_prefix, attrname), i)) + f_consts.write(" %s = %d" % (code_encoder(attrname), i)) print >>f_consts, "\n };" print >>f_consts, "enum %s {" % encode_symbol(pos_prefix) @@ -645,7 +653,7 @@ print >>f_consts, "," else: first = False - f_consts.write(" %s = %d" % (encode_symbol(pos_prefix, attrname), i)) + f_consts.write(" %s = %d" % (pos_encoder(attrname), i)) print >>f_consts, "\n };" def write_table(self, f_decls, f_defs, table_name, structure_size, table): @@ -730,7 +738,7 @@ print >>f_decls, "extern __obj %s;\n" % encode_path(structure_name) is_class = path and self.importer.get_object(path).has_kind("") - pos = is_class and encode_symbol("pos", encode_type_attribute(path)) or "0" + pos = is_class and encode_pos(encode_type_attribute(path)) or "0" print >>f_defs, """\ __obj %s = { @@ -833,7 +841,7 @@ if attrname is None: table.append("0") else: - table.append(encode_symbol("code", attrname)) + table.append(encode_code(attrname)) def populate_parameter_table(self, parameters, table): @@ -940,8 +948,8 @@ # Special internal key member. elif attrname == "__key__": - structure.append("{.code=%s, .pos=%s}" % (attr and encode_symbol("code", attr) or "0", - attr and encode_symbol("pos", attr) or "0")) + structure.append("{.code=%s, .pos=%s}" % (attr and encode_code(attr) or "0", + attr and encode_pos(attr) or "0")) continue # Special cases. @@ -1187,10 +1195,10 @@ return 0; } """ % ( - encode_symbol("pos", "value"), - encode_symbol("pos", "__data__"), + encode_pos("value"), + encode_pos("__data__"), encode_function_pointer("__builtins__.str.str"), - encode_symbol("pos", "__data__") + encode_pos("__data__") ) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 5a69a5a45afb -r 3bba67784f79 templates/ops.h --- a/templates/ops.h Mon Feb 27 00:55:52 2017 +0100 +++ b/templates/ops.h Mon Feb 27 11:57:15 2017 +0100 @@ -115,11 +115,6 @@ unsigned int __TYPECODE(__ref obj); unsigned int __TYPEPOS(__ref obj); -/* Attribute codes and positions for attribute names. */ - -#define __ATTRCODE(__ATTRNAME) (__code_##__ATTRNAME) -#define __ATTRPOS(__ATTRNAME) (__pos_##__ATTRNAME) - /* Memory allocation. */ void *__ALLOCATE(size_t nmemb, size_t size); diff -r 5a69a5a45afb -r 3bba67784f79 templates/types.h --- a/templates/types.h Mon Feb 27 00:55:52 2017 +0100 +++ b/templates/types.h Mon Feb 27 11:57:15 2017 +0100 @@ -113,9 +113,16 @@ typedef __attr (*__func)(); -/* Convenience macros. */ +/* Argument lists. */ #define __ARGS(...) ((__attr[]) {__VA_ARGS__}) #define __KWARGS(...) ((__param[]) {__VA_ARGS__}) +/* Attribute codes and positions for attribute names. */ + +#define __ATTRCODE(ATTRNAME) __code_##ATTRNAME +#define __ATTRPOS(ATTRNAME) __pos_##ATTRNAME +#define __PARAMCODE(PARAMNAME) __pcode_##PARAMNAME +#define __PARAMPOS(PARAMNAME) __ppos_##PARAMNAME + #endif /* __TYPES_H__ */ diff -r 5a69a5a45afb -r 3bba67784f79 translator.py --- a/translator.py Mon Feb 27 00:55:52 2017 +0100 +++ b/translator.py Mon Feb 27 11:57:15 2017 +0100 @@ -23,10 +23,10 @@ first, get_builtin_class, init_item, is_newer, \ predefined_constants from encoders import encode_access_instruction, encode_access_instruction_arg, \ - encode_function_pointer, encode_literal_constant, \ + encode_code, encode_function_pointer, encode_literal_constant, \ encode_literal_instantiator, encode_instantiator_pointer, \ - encode_instructions, \ - encode_path, encode_symbol, encode_type_attribute, \ + encode_instructions, encode_path, encode_pos, \ + encode_symbol, encode_type_attribute, \ is_type_attribute from errors import InspectError, TranslateError from os.path import exists, join @@ -142,7 +142,7 @@ elif parent: return "__store_via_object(&%s, %s, %s)" % ( - encode_path(parent), encode_symbol("pos", attrname), self.expr) + encode_path(parent), encode_pos(attrname), self.expr) # All other assignments involve the names as they were given. @@ -160,7 +160,7 @@ elif parent: return "__load_via_object(&%s, %s)" % ( - encode_path(parent), encode_symbol("pos", attrname)) + encode_path(parent), encode_pos(attrname)) # All other accesses involve the names as they were given. @@ -979,8 +979,8 @@ parent, attrname = path.rsplit(".", 1) self.writestmt("__store_via_object(&%s, %s, __load_via_object(&%s, %s));" % ( - encode_path(class_name), encode_symbol("pos", name), - encode_path(parent), encode_symbol("pos", attrname) + encode_path(class_name), encode_pos(name), + encode_path(parent), encode_pos(attrname) )) def process_from_node(self, n): @@ -1064,7 +1064,7 @@ elif guard == "common": ref = first(self.deducer.accessor_all_general_types[location]) typeattr = encode_type_attribute(ref.get_origin()) - argstr = "%s, %s" % (encode_symbol("pos", typeattr), encode_symbol("code", typeattr)) + argstr = "%s, %s" % (encode_pos(typeattr), encode_code(typeattr)) else: return @@ -1369,8 +1369,7 @@ else: kwargs.append(str(argexpr)) kwcodes.append("{%s, %s}" % ( - encode_symbol("ppos", arg.name), - encode_symbol("pcode", arg.name))) + encode_ppos(arg.name), encode_pcode(arg.name))) # Store non-keyword arguments in the argument list, rejecting # superfluous arguments. @@ -1449,7 +1448,7 @@ target_var, target_var)) else: stages.append("__load_via_object(%s.value, %s).fn" % ( - target_var, encode_symbol("pos", "__fn__"))) + target_var, encode_pos("__fn__"))) # With a known target, the function is obtained directly and called. # By putting the invocation at the end of the final element in the