# HG changeset patch # User Paul Boddie # Date 1490480416 -3600 # Node ID 287b154329b01489dc69dfd603e685f0c84e7379 # Parent e6439e6c26471ed264d69ce3db83bc61cf39599f Made type-specific literal instantiation functions where the tuple instantiator allocates the tuple attributes and elements at the same time. diff -r e6439e6c2647 -r 287b154329b0 generator.py --- a/generator.py Sat Mar 25 23:06:55 2017 +0100 +++ b/generator.py Sat Mar 25 23:20:16 2017 +0100 @@ -42,10 +42,13 @@ # NOTE: These must be synchronised with the library. + dict_type = "__builtins__.dict.dict" function_type = "__builtins__.core.function" int_type = "__builtins__.int.int" + list_type = "__builtins__.list.list" none_type = "__builtins__.none.NoneType" string_type = "__builtins__.str.string" + tuple_type = "__builtins__.tuple.tuple" type_type = "__builtins__.core.type" unicode_type = "__builtins__.unicode.utf8string" @@ -58,17 +61,10 @@ ("__builtins__.notimplemented", "NotImplemented"), ) - literal_mapping_types = ( - "__builtins__.dict.dict", + literal_instantiator_types = ( + dict_type, list_type, tuple_type ) - literal_sequence_types = ( - "__builtins__.list.list", - "__builtins__.tuple.tuple", - ) - - literal_instantiator_types = literal_mapping_types + literal_sequence_types - def __init__(self, importer, optimiser, output): """ @@ -1247,15 +1243,11 @@ # Signature: __newliteral_sequence(ARGS, NUM) if path in self.literal_instantiator_types: - if path in self.literal_mapping_types: - style = "mapping" - else: - style = "sequence" + style = path.rsplit(".", 1)[-1] - print >>f_signatures, "#define %s(ARGS, NUM) (%s(__NEWINSTANCE(%s), ARGS, NUM))" % ( + print >>f_signatures, "#define %s(ARGS, NUM) (%s(ARGS, NUM))" % ( encode_literal_instantiator(path), encode_literal_data_initialiser(style), - encode_path(path) ) def write_main_program(self, f_code, f_signatures): diff -r e6439e6c2647 -r 287b154329b0 templates/progops.c --- a/templates/progops.c Sat Mar 25 23:06:55 2017 +0100 +++ b/templates/progops.c Sat Mar 25 23:20:16 2017 +0100 @@ -28,12 +28,17 @@ /* Generic instantiation operations, defining common members. */ +void __init(__ref obj, const __table * table, __ref cls) +{ + obj->table = table; + obj->pos = __INSTANCEPOS; + __store_via_object(obj, __class__, __ATTRVALUE(cls)); +} + __attr __new(const __table * table, __ref cls, size_t size, int immutable) { __ref obj = (__ref) (immutable ? __ALLOCATEIM : __ALLOCATE)(1, size); - obj->table = table; - obj->pos = __INSTANCEPOS; - __store_via_object(obj, __class__, __ATTRVALUE(cls)); + __init(obj, table, cls); return __ATTRVALUE(obj); } @@ -57,11 +62,8 @@ return data; } -__attr __newdata_sequence(__attr self, __attr args[], unsigned int number) +void __newdata_sequence(__attr self, __attr args[], unsigned int number, __fragment *data) { - /* Calculate the size of the fragment. */ - - __fragment *data = __new_fragment(number); __attr attr = {.seqvalue=data}; unsigned int i; @@ -75,12 +77,35 @@ /* Store a reference to the data in the object's __data__ attribute. */ __store_via_object(__VALUE(self), __data__, attr); +} + +__attr __newdata_list(__attr args[], unsigned int number) +{ + __attr self = __NEWINSTANCE(__builtins___list_list); + __fragment *data = __new_fragment(number); + __newdata_sequence(self, args, number, data); + return self; +} + +__attr __newdata_tuple(__attr args[], unsigned int number) +{ + /* Allocate the tuple and fragment together. */ + + __ref obj = (__ref) __ALLOCATE(1, __INSTANCESIZE(__builtins___tuple_tuple) + __FRAGMENT_SIZE(number)); + __attr self = __ATTRVALUE(obj); + + /* Initialise the instance and fragment. */ + + __init(obj, &__INSTANCETABLE(__builtins___tuple_tuple), &__builtins___tuple_tuple); + __newdata_sequence(self, args, number, (__fragment *) ((void *) obj + __INSTANCESIZE(__builtins___tuple_tuple))); return self; } #ifdef __HAVE___builtins___dict_dict -__attr __newdata_mapping(__attr self, __attr args[], unsigned int number) +__attr __newdata_dict(__attr args[], unsigned int number) { + __attr self = __NEWINSTANCE(__builtins___dict_dict); + /* Create a temporary list using the arguments. */ __attr tmp = __newliteral___builtins___list_list(args, number); diff -r e6439e6c2647 -r 287b154329b0 templates/progops.h --- a/templates/progops.h Sat Mar 25 23:06:55 2017 +0100 +++ b/templates/progops.h Sat Mar 25 23:20:16 2017 +0100 @@ -32,10 +32,11 @@ __fragment *__new_fragment(unsigned int n); -__attr __newdata_sequence(__attr self, __attr args[], unsigned int number); +__attr __newdata_list(__attr args[], unsigned int number); +__attr __newdata_tuple(__attr args[], unsigned int number); #ifdef __HAVE___builtins___dict_dict -__attr __newdata_mapping(__attr self, __attr args[], unsigned int number); +__attr __newdata_dict(__attr args[], unsigned int number); #endif /* __HAVE___builtins___dict_dict */ /* Helpers for raising errors within common operations. */ @@ -78,8 +79,10 @@ /* Convenience definitions. */ -#define __NEWINSTANCE(__CLS) __new(&__InstanceTable_##__CLS, &__CLS, sizeof(__obj_##__CLS), 0) -#define __NEWINSTANCEIM(__CLS) __new(&__InstanceTable_##__CLS, &__CLS, sizeof(__obj_##__CLS), 1) -#define __ISINSTANCE(__ATTR, __TYPE) __BOOL(__fn_native_introspection_isinstance(__NULL, __ATTR, __TYPE)) +#define __INSTANCESIZE(CLS) sizeof(__obj_##CLS) +#define __INSTANCETABLE(CLS) (__InstanceTable_##CLS) +#define __NEWINSTANCE(CLS) __new(&__INSTANCETABLE(CLS), &CLS, __INSTANCESIZE(CLS), 0) +#define __NEWINSTANCEIM(CLS) __new(&__INSTANCETABLE(CLS), &CLS, __INSTANCESIZE(CLS), 1) +#define __ISINSTANCE(ATTR, TYPE) __BOOL(__fn_native_introspection_isinstance(__NULL, ATTR, TYPE)) #endif /* __PROGOPS_H__ */