# HG changeset patch # User Paul Boddie # Date 1479832599 -3600 # Node ID 47ab3fff828ba29885dc6d1334661f26951d28a6 # Parent fe05975a4b54e3062c1e9a4a79574e6522e0c183 Switched from using macro aliases to constant pointer aliases (missing C++-style references for this purpose). diff -r fe05975a4b54 -r 47ab3fff828b templates/native.c --- a/templates/native.c Tue Nov 22 17:20:56 2016 +0100 +++ b/templates/native.c Tue Nov 22 17:36:39 2016 +0100 @@ -33,258 +33,224 @@ __attr __fn_native__exit(__attr __args[]) { - #define status (__args[1]) + __attr * const status = &__args[1]; - exit(__load_via_object(status.value, __pos___data__).intvalue); + exit(__load_via_object(status->value, __pos___data__).intvalue); return __builtins___none_None; - #undef status } __attr __fn_native__get_argv(__attr __args[]) { - #define status (__args[1]) + __attr * const status = &__args[1]; /* NOTE: To be written. */ return __builtins___none_None; - #undef status } __attr __fn_native__get_path(__attr __args[]) { - #define status (__args[1]) + __attr * const status = &__args[1]; /* NOTE: To be written. */ return __builtins___none_None; - #undef status } __attr __fn_native__is(__attr __args[]) { - #define x (__args[1]) - #define y (__args[2]) + __attr * const x = &__args[1]; + __attr * const y = &__args[2]; - return x.value == y.value ? __builtins___boolean_True : __builtins___boolean_False; - #undef x - #undef y + return x->value == y->value ? __builtins___boolean_True : __builtins___boolean_False; } __attr __fn_native__is_not(__attr __args[]) { - #define x (__args[1]) - #define y (__args[2]) + __attr * const x = &__args[1]; + __attr * const y = &__args[2]; - return x.value != y.value ? __builtins___boolean_True : __builtins___boolean_False; - #undef x - #undef y + return x->value != y->value ? __builtins___boolean_True : __builtins___boolean_False; } __attr __fn_native__int_add(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i + j); - #undef self - #undef other } __attr __fn_native__int_sub(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i - j); - #undef self - #undef other } __attr __fn_native__int_mul(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i * j); - #undef self - #undef other } __attr __fn_native__int_div(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i / j); - #undef self - #undef other } __attr __fn_native__int_mod(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i % j); - #undef self - #undef other } __attr __fn_native__int_neg(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; /* Return the new integer. */ return __new_int(-i); - #undef self } __attr __fn_native__int_pow(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int((int) pow(i, j)); - #undef self - #undef other } __attr __fn_native__int_and(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i & j); - #undef self - #undef other } __attr __fn_native__int_or(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i | j); - #undef self - #undef other } __attr __fn_native__int_xor(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return the new integer. */ /* NOTE: No overflow test applied. */ return __new_int(i ^ j); - #undef self - #undef other } __attr __fn_native__int_lt(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return a boolean result. */ return i < j ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__int_gt(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return a boolean result. */ return i > j ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__int_eq(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return a boolean result. */ return i == j ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__int_ne(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__ and other.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; - int j = __load_via_object(other.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; + int j = __load_via_object(other->value, __pos___data__).intvalue; /* Return a boolean result. */ return i != j ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__int_str(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as int */ - int i = __load_via_object(self.value, __pos___data__).intvalue; + int i = __load_via_object(self->value, __pos___data__).intvalue; int n = i != 0 ? (int) ceil(log10(i+1)) + 1 : 2; char *s = calloc(n, sizeof(char)); @@ -293,17 +259,15 @@ /* Return a new string. */ return __new_str(s); - #undef self - #undef other } __attr __fn_native__str_add(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__, other.__data__ interpreted as string */ - char *s = __load_via_object(self.value, __pos___data__).strvalue; - char *o = __load_via_object(other.value, __pos___data__).strvalue; + char *s = __load_via_object(self->value, __pos___data__).strvalue; + char *o = __load_via_object(other->value, __pos___data__).strvalue; int n = strlen(s) + strlen(o) + 1; char *r = calloc(n, sizeof(char)); @@ -312,78 +276,68 @@ /* Return a new string. */ return __new_str(r); - #undef self - #undef other } __attr __fn_native__str_lt(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__, other.__data__ interpreted as string */ - char *s = __load_via_object(self.value, __pos___data__).strvalue; - char *o = __load_via_object(other.value, __pos___data__).strvalue; + char *s = __load_via_object(self->value, __pos___data__).strvalue; + char *o = __load_via_object(other->value, __pos___data__).strvalue; /* NOTE: Using simple byte-level string operations. */ return strcmp(s, o) < 0 ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__str_gt(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__, other.__data__ interpreted as string */ - char *s = __load_via_object(self.value, __pos___data__).strvalue; - char *o = __load_via_object(other.value, __pos___data__).strvalue; + char *s = __load_via_object(self->value, __pos___data__).strvalue; + char *o = __load_via_object(other->value, __pos___data__).strvalue; /* NOTE: Using simple byte-level string operations. */ return strcmp(s, o) > 0 ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__str_eq(__attr __args[]) { - #define self (__args[1]) - #define other (__args[2]) + __attr * const self = &__args[1]; + __attr * const other = &__args[2]; /* self.__data__, other.__data__ interpreted as string */ - char *s = __load_via_object(self.value, __pos___data__).strvalue; - char *o = __load_via_object(other.value, __pos___data__).strvalue; + char *s = __load_via_object(self->value, __pos___data__).strvalue; + char *o = __load_via_object(other->value, __pos___data__).strvalue; /* NOTE: Using simple byte-level string operations. */ return strcmp(s, o) == 0 ? __builtins___boolean_True : __builtins___boolean_False; - #undef self - #undef other } __attr __fn_native__str_len(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as string */ - char *s = __load_via_object(self.value, __pos___data__).strvalue; + char *s = __load_via_object(self->value, __pos___data__).strvalue; /* Return the new integer. */ return __new_int(strlen(s)); - #undef self } __attr __fn_native__str_nonempty(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as string */ - char *s = __load_via_object(self.value, __pos___data__).strvalue; + char *s = __load_via_object(self->value, __pos___data__).strvalue; return strlen(s) ? __builtins___boolean_True : __builtins___boolean_False; - #undef self } __attr __fn_native__list_init(__attr __args[]) { - #define __size (__args[1]) - /* __size.__data__ interpreted as int */ - unsigned int n = __load_via_object(__size.value, __pos___data__).intvalue; + __attr * const size = &__args[1]; + /* size.__data__ interpreted as int */ + unsigned int n = __load_via_object(size->value, __pos___data__).intvalue; /* Allocate space for the list. */ __fragment *data = calloc(1, __FRAGMENT_SIZE(n)); @@ -393,15 +347,14 @@ data->size = 0; data->capacity = n; return attr; - #undef __size } __attr __fn_native__list_append(__attr __args[]) { - #define self (__args[1]) - #define __value (__args[2]) + __attr * const self = &__args[1]; + __attr * const value = &__args[2]; /* self.__data__ interpreted as list */ - __fragment *data = __load_via_object(self.value, __pos___data__).data; + __fragment *data = __load_via_object(self->value, __pos___data__).data; unsigned int size = data->size, capacity = data->capacity; unsigned int n; @@ -415,20 +368,18 @@ } /* Insert the new element and increment the list size. */ - data->attrs[size] = __value; + data->attrs[size] = *value; data->size = size + 1; return __builtins___none_None; - #undef self - #undef __value } __attr __fn_native__list_concat(__attr __args[]) { - #define self (__args[1]) - #define __other (__args[2]) + __attr * const self = &__args[1]; + __attr * const __other = &__args[2]; /* self.__data__, __other.__data__ interpreted as list */ - __fragment *data = __load_via_object(self.value, __pos___data__).data; - __fragment *other_data = __load_via_object(__other.value, __pos___data__).data; + __fragment *data = __load_via_object(self->value, __pos___data__).data; + __fragment *other_data = __load_via_object(__other->value, __pos___data__).data; unsigned int size = data->size, capacity = data->capacity; unsigned int other_size = other_data->size; unsigned int i, j, n; @@ -446,57 +397,50 @@ data->attrs[i] = other_data->attrs[j]; data->size = n; return __builtins___none_None; - #undef self - #undef __other } __attr __fn_native__list_len(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as fragment */ - unsigned int size = __load_via_object(self.value, __pos___data__).data->size; + unsigned int size = __load_via_object(self->value, __pos___data__).data->size; /* Return the new integer. */ return __new_int(size); - #undef self } __attr __fn_native__list_nonempty(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; - return __load_via_object(self.value, __pos___data__).data->size ? __builtins___boolean_True : __builtins___boolean_False; - #undef self + return __load_via_object(self->value, __pos___data__).data->size ? __builtins___boolean_True : __builtins___boolean_False; } __attr __fn_native__list_element(__attr __args[]) { - #define self (__args[1]) - #define index (__args[2]) + __attr * const self = &__args[1]; + __attr * const index = &__args[2]; /* self.__data__ interpreted as fragment */ - __attr *elements = __load_via_object(self.value, __pos___data__).data->attrs; + __attr *elements = __load_via_object(self->value, __pos___data__).data->attrs; /* index.__data__ interpreted as int */ - int i = __load_via_object(index.value, __pos___data__).intvalue; + int i = __load_via_object(index->value, __pos___data__).intvalue; return elements[i]; - #undef self - #undef index } __attr __fn_native__list_to_tuple(__attr __args[]) { - #define l (__args[1]) + __attr * const l = &__args[1]; /* NOTE: To be written. */ return __builtins___none_None; - #undef l } __attr __fn_native__buffer_str(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as buffer */ - __fragment *data = __load_via_object(self.value, __pos___data__).data; + __fragment *data = __load_via_object(self->value, __pos___data__).data; unsigned int size = 0, i, j; char *s; @@ -513,82 +457,71 @@ /* Return a new string. */ return __new_str(s); - #undef self } __attr __fn_native__tuple_init(__attr __args[]) { - #define size (__args[1]) + __attr * const size = &__args[1]; /* size.__data__ interpreted as fragment */ - __fragment *data = calloc(__load_via_object(size.value, __pos___data__).intvalue, sizeof(__attr)); + __fragment *data = calloc(__load_via_object(size->value, __pos___data__).intvalue, sizeof(__attr)); __attr attr = {0, .data=data}; return attr; - #undef size } __attr __fn_native__tuple_len(__attr __args[]) { - #define self (__args[1]) + __attr * const self = &__args[1]; /* self.__data__ interpreted as fragment */ - unsigned int size = __load_via_object(self.value, __pos___data__).data->size; + unsigned int size = __load_via_object(self->value, __pos___data__).data->size; /* Return the new integer. */ return __new_int(size); - #undef self } __attr __fn_native__tuple_element(__attr __args[]) { - #define self (__args[1]) - #define index (__args[2]) + __attr * const self = &__args[1]; + __attr * const index = &__args[2]; /* self.__data__ interpreted as fragment */ - __attr *elements = __load_via_object(self.value, __pos___data__).data->attrs; + __attr *elements = __load_via_object(self->value, __pos___data__).data->attrs; /* index.__data__ interpreted as int */ - int i = __load_via_object(index.value, __pos___data__).intvalue; + int i = __load_via_object(index->value, __pos___data__).intvalue; return elements[i]; - #undef self - #undef index } __attr __fn_native__isinstance(__attr __args[]) { - #define obj (__args[1]) - #define cls (__args[2]) + __attr * const obj = &__args[1]; + __attr * const cls = &__args[2]; - if (__is_instance(obj.value) && __HASATTR(__get_class(obj.value), __TYPEPOS(cls.value), __TYPECODE(cls.value))) + if (__is_instance(obj->value) && __HASATTR(__get_class(obj->value), __TYPEPOS(cls->value), __TYPECODE(cls->value))) return __builtins___boolean_True; else return __builtins___boolean_False; - #undef obj - #undef cls } __attr __fn_native__read(__attr __args[]) { - #define fd (__args[1]) - #define n (__args[2]) + __attr * const fd = &__args[1]; + __attr * const n = &__args[2]; /* NOTE: To be written. */ return __builtins___none_None; - #undef fd - #undef n } __attr __fn_native__write(__attr __args[]) { - #define fd (__args[1]) - #define str (__args[2]) + __attr * const fd = &__args[1]; + __attr * const str = &__args[2]; /* fd.__data__ interpreted as int */ - int i = __load_via_object(fd.value, __pos___data__).intvalue; + int i = __load_via_object(fd->value, __pos___data__).intvalue; /* str.__data__ interpreted as string */ - char *s = __load_via_object(str.value, __pos___data__).strvalue; + char *s = __load_via_object(str->value, __pos___data__).strvalue; write(i, s, sizeof(char) * strlen(s)); return __builtins___none_None; - #undef fd - #undef str } /* Module initialisation. */ diff -r fe05975a4b54 -r 47ab3fff828b translator.py --- a/translator.py Tue Nov 22 17:20:56 2016 +0100 +++ b/translator.py Tue Nov 22 17:36:39 2016 +0100 @@ -78,6 +78,10 @@ "A reference to a name in the translation." + def __init__(self, name, ref, expr=None, parameter=None): + results.ResolvedNameRef.__init__(self, name, ref, expr) + self.parameter = parameter + def __str__(self): "Return an output representation of the referenced name." @@ -114,7 +118,7 @@ # All other assignments involve the names as they were given. else: - return "%s = %s" % (attrname, self.expr) + return "(%s%s) = %s" % (self.parameter and "*" or "", attrname, self.expr) # Expressions. @@ -132,7 +136,7 @@ # All other accesses involve the names as they were given. else: - return attrname + return "(%s%s)" % (self.parameter and "*" or "", attrname) class TrConstantValueRef(results.ConstantValueRef, TranslationResult): @@ -298,6 +302,12 @@ class_name, method_name = path.rsplit(".", 1) return self.importer.classes.has_key(class_name) and class_name + def in_method(self): + + "Return whether the current namespace provides a method." + + return self.in_function and self.is_method(self.get_namespace_path()) + # Namespace recording. def record_namespaces(self, node): @@ -1089,11 +1099,18 @@ locals = self.importer.function_locals.get(self.get_namespace_path()) ref = locals and locals.get(n.name) + # Determine whether the name refers to a parameter. The generation of + # parameter references is different from other names. + + parameters = self.importer.function_parameters.get(self.get_namespace_path()) + parameter = n.name == "self" and self.in_method() or \ + parameters and n.name in parameters + # Qualified names are used for resolved static references or for # static namespace members. The reference should be configured to return # such names. - return TrResolvedNameRef(n.name, ref, expr=expr) + return TrResolvedNameRef(n.name, ref, expr=expr, parameter=parameter) def process_not_node(self, n): @@ -1373,13 +1390,12 @@ names.sort() self.writeline("__attr %s;" % ", ".join(names)) - self.write_parameters(name, True) + self.write_parameters(name) def end_function(self, name): "End the function having the given 'name'." - self.write_parameters(name, False) self.indent -= 1 print >>self.out, "}" print >>self.out @@ -1397,12 +1413,11 @@ self.writeline("__attr %s__tmp_result;" % targets) self.writeline("__exc __tmp_exc;") - def write_parameters(self, name, define=True): + def write_parameters(self, name): """ For the function having the given 'name', write definitions of - parameters found in the arguments array if 'define' is set to a true - value, or write "undefinitions" if 'define' is set to a false value. + parameters found in the arguments array. """ parameters = self.importer.function_parameters[name] @@ -1410,18 +1425,12 @@ # Generate any self reference. if self.is_method(name): - if define: - self.writeline("#define self (__args[0])") - else: - self.writeline("#undef self") + self.writeline("__attr * const self = &__args[0];") # Generate aliases for the parameters. for i, parameter in enumerate(parameters): - if define: - self.writeline("#define %s (__args[%d])" % (encode_path(parameter), i+1)) - else: - self.writeline("#undef %s" % encode_path(parameter)) + self.writeline("__attr * const %s = &__args[%d];" % (encode_path(parameter), i+1)) def start_if(self, first, test_ref): self.writestmt("%sif (__BOOL(%s))" % (not first and "else " or "", test_ref))