1.1 --- a/generator.py Thu Nov 04 23:44:29 2021 +0100
1.2 +++ b/generator.py Tue Aug 29 01:46:23 2023 +0200
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Generate C code from object layouts and other deduced information.
1.6
1.7 -Copyright (C) 2015-2019, 2021 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2015-2019, 2021, 2023 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -40,6 +40,10 @@
1.13
1.14 "A code generator."
1.15
1.16 + # Generated functions employ result target and context parameters.
1.17 +
1.18 + extra_args = 2
1.19 +
1.20 # NOTE: These must be synchronised with the library.
1.21
1.22 dict_type = "__builtins__.dict.dict"
1.23 @@ -315,11 +319,12 @@
1.24 self.write_instance_structure(f_decls, path)
1.25 extra_function_instances.append(path)
1.26
1.27 - # Write function declarations.
1.28 - # Signature: __attr <name>(...);
1.29 + # Write function declarations, including result target and
1.30 + # context parameters.
1.31 + # Signature: __attr <name>(__attr, __attr, ...);
1.32
1.33 parameters = self.importer.function_parameters[path]
1.34 - l = ["__attr"] * (len(parameters) + 1)
1.35 + l = ["__attr"] * (len(parameters) + self.extra_args)
1.36 print >>f_signatures, "__attr %s(%s);" % (encode_function_pointer(path), ", ".join(l))
1.37
1.38 # Generate parameter table size data.
1.39 @@ -903,7 +908,7 @@
1.40
1.41 parameters = self.importer.function_parameters[path]
1.42 defaults = self.importer.function_defaults.get(path)
1.43 - num_parameters = len(parameters) + 1
1.44 + num_parameters = len(parameters) + self.extra_args
1.45 return num_parameters - (defaults and len(defaults) or 0), num_parameters
1.46
1.47 def get_static_attributes(self, kind, name, attrnames):
1.48 @@ -1270,9 +1275,9 @@
1.49
1.50 if path == self.int_type:
1.51 print >>f_code, """\
1.52 -__attr %s(__attr __self, __attr number_or_string, __attr base)
1.53 +__attr %s(__attr __result, __attr __self, __attr number_or_string, __attr base)
1.54 {
1.55 - return __fn___builtins___int_new_int(__NULL, number_or_string, base);
1.56 + return __fn___builtins___int_new_int(__NULL, __NULL, number_or_string, base);
1.57 }
1.58 """ % (
1.59 encode_instantiator_pointer(path),
1.60 @@ -1285,9 +1290,9 @@
1.61
1.62 elif path == self.string_type:
1.63 print >>f_code, """\
1.64 -__attr %s(__attr __self, __attr obj)
1.65 +__attr %s(__attr __result, __attr __self, __attr obj)
1.66 {
1.67 - return __fn___builtins___str_new_str(__NULL, obj);
1.68 + return __fn___builtins___str_new_str(__NULL, __NULL, obj);
1.69 }
1.70 """ % (
1.71 encode_instantiator_pointer(path),
1.72 @@ -1297,9 +1302,9 @@
1.73
1.74 else:
1.75 print >>f_code, """\
1.76 -__attr %s(__attr __self%s)
1.77 +__attr %s(__attr __result, __attr __self%s)
1.78 {
1.79 - return %s(__NEWINSTANCE(%s)%s);
1.80 + return %s(__NULL, __NEWINSTANCE(%s)%s);
1.81 }
1.82 """ % (
1.83 encode_instantiator_pointer(path),
1.84 @@ -1309,9 +1314,9 @@
1.85 parameters and ", %s" % ", ".join(parameters) or ""
1.86 )
1.87
1.88 - # Signature: __new_typename(__attr __self, ...)
1.89 + # Signature: __new_typename(__attr __result, __attr __self, ...)
1.90
1.91 - print >>f_signatures, "__attr %s(__attr __self%s);" % (
1.92 + print >>f_signatures, "__attr %s(__attr __result, __attr __self%s);" % (
1.93 encode_instantiator_pointer(path),
1.94 l and ", %s" % ", ".join(l) or ""
1.95 )
1.96 @@ -1358,7 +1363,7 @@
1.97
1.98 fprintf(stderr, "Program terminated due to exception: %%s.\\n",
1.99 __load_via_object(
1.100 - __VALUE(%s(__NULL, __tmp_exc.arg)),
1.101 + __VALUE(%s(__NULL, __NULL, __tmp_exc.arg)),
1.102 __data__).strvalue);
1.103 return 1;
1.104 }