# HG changeset patch # User Paul Boddie # Date 1477947218 -3600 # Node ID 49376f3ab03dea1df169bc151903bde88cc1ce14 # Parent 6a86d0599fd0effba315dd91921bca78c6cbe1e4 Replace the native module code with actual native template code. Refined the use of native code for list and tuple initialisation. diff -r 6a86d0599fd0 -r 49376f3ab03d generator.py --- a/generator.py Mon Oct 31 21:50:41 2016 +0100 +++ b/generator.py Mon Oct 31 21:53:38 2016 +0100 @@ -886,8 +886,9 @@ print >>f_signatures, "void %s();" % function_name # Emit the main module's function last. + # Omit the native module. - if name != "__main__": + if name not in ("__main__", "native"): print >>f_code, """\ %s();""" % function_name diff -r 6a86d0599fd0 -r 49376f3ab03d lib/__builtins__/list.py --- a/lib/__builtins__/list.py Mon Oct 31 21:50:41 2016 +0100 +++ b/lib/__builtins__/list.py Mon Oct 31 21:53:38 2016 +0100 @@ -33,7 +33,7 @@ # Reserve space for a fragment reference. - self.__data__ = native._list_init(args) # reserve space for elements + self.__data__ = native._list_init(len(args)) # reserve space for elements if args is not None: self.extend(args) diff -r 6a86d0599fd0 -r 49376f3ab03d lib/__builtins__/tuple.py --- a/lib/__builtins__/tuple.py Mon Oct 31 21:50:41 2016 +0100 +++ b/lib/__builtins__/tuple.py Mon Oct 31 21:53:38 2016 +0100 @@ -31,7 +31,7 @@ "Initialise the tuple." - self.__data__ = native._tuple_init(args) # allocate and copy elements + self.__data__ = native._tuple_init(args, len(args)) # allocate and copy elements def __getitem__(self, index): diff -r 6a86d0599fd0 -r 49376f3ab03d lib/native.py --- a/lib/native.py Mon Oct 31 21:50:41 2016 +0100 +++ b/lib/native.py Mon Oct 31 21:53:38 2016 +0100 @@ -48,14 +48,14 @@ def _str_len(self): pass def _str_nonempty(self): pass -def _list_init(args): pass +def _list_init(size): pass def _list_len(self): pass def _list_nonempty(self): pass def _list_element(self, index): pass def _list_to_tuple(l): pass -def _tuple_init(args): pass +def _tuple_init(args, size): pass def _tuple_len(self): pass def _tuple_element(self, index): pass diff -r 6a86d0599fd0 -r 49376f3ab03d templates/Makefile --- a/templates/Makefile Mon Oct 31 21:50:41 2016 +0100 +++ b/templates/Makefile Mon Oct 31 21:53:38 2016 +0100 @@ -1,4 +1,4 @@ -SRC = exceptions.c main.c ops.c progops.c progtypes.c $(wildcard src/*.c) +SRC = exceptions.c main.c native.c ops.c progops.c progtypes.c $(wildcard src/*.c) OBJ = $(SRC:.c=.o) CFLAGS = -I. diff -r 6a86d0599fd0 -r 49376f3ab03d templates/native.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/native.c Mon Oct 31 21:53:38 2016 +0100 @@ -0,0 +1,363 @@ +#include +#include "types.h" +#include "exceptions.h" +#include "ops.h" +#include "progconsts.h" +#include "progops.h" +#include "progtypes.h" +#include "main.h" + +__attr __fn_native__is(__attr __args[]) +{ + #define x (__args[1]) + #define y (__args[2]) + + return x.value == y.value ? __builtins___boolean_True : __builtins___boolean_False; + #undef x + #undef y +} + +__attr __fn_native__is_not(__attr __args[]) +{ + #define x (__args[1]) + #define y (__args[2]) + + return x.value != y.value ? __builtins___boolean_True : __builtins___boolean_False; + #undef x + #undef y +} + +__attr __fn_native__int_add(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_sub(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_mul(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_div(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_mod(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_pow(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_and(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_or(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_xor(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_rsub(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_rdiv(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_rmod(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_rpow(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_lt(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_gt(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__int_eq(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__str_add(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__str_lt(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__str_gt(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__str_eq(__attr __args[]) +{ + #define self (__args[1]) + #define other (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef other +} + +__attr __fn_native__str_len(__attr __args[]) +{ + #define self (__args[1]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self +} + +__attr __fn_native__str_nonempty(__attr __args[]) +{ + #define self (__args[1]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self +} + +__attr __fn_native__list_init(__attr __args[]) +{ + #define size (__args[1]) + /* size.__data__ interpreted as int */ + __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__list_len(__attr __args[]) +{ + #define self (__args[1]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self +} + +__attr __fn_native__list_nonempty(__attr __args[]) +{ + #define self (__args[1]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self +} + +__attr __fn_native__list_element(__attr __args[]) +{ + #define self (__args[1]) + #define index (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef index +} + +__attr __fn_native__list_to_tuple(__attr __args[]) +{ + #define l (__args[1]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef l +} + +__attr __fn_native__tuple_init(__attr __args[]) +{ + #define size (__args[1]) + /* size.__data__ interpreted as int */ + __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]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self +} + +__attr __fn_native__tuple_element(__attr __args[]) +{ + #define self (__args[1]) + #define index (__args[2]) + + /* NOTE: To be written. */ + return __builtins___none_None; + #undef self + #undef index +} + +__attr __fn_native__isinstance(__attr __args[]) +{ + #define obj (__args[1]) + #define cls (__args[2]) + + if (__HASATTR(obj.value, __TYPEPOS(cls.value), __TYPECODE(cls.value))) + return obj; + else + return __builtins___none_None; + #undef obj + #undef cls +} + +void __main_native() +{ +} diff -r 6a86d0599fd0 -r 49376f3ab03d templates/native.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/native.h Mon Oct 31 21:53:38 2016 +0100 @@ -0,0 +1,39 @@ +#ifndef __NATIVE_H__ +#define __NATIVE_H__ + +__attr __fn_native__is(__attr __args[]); +__attr __fn_native__is_not(__attr __args[]); +__attr __fn_native__int_add(__attr __args[]); +__attr __fn_native__int_sub(__attr __args[]); +__attr __fn_native__int_mul(__attr __args[]); +__attr __fn_native__int_div(__attr __args[]); +__attr __fn_native__int_mod(__attr __args[]); +__attr __fn_native__int_pow(__attr __args[]); +__attr __fn_native__int_and(__attr __args[]); +__attr __fn_native__int_or(__attr __args[]); +__attr __fn_native__int_xor(__attr __args[]); +__attr __fn_native__int_rsub(__attr __args[]); +__attr __fn_native__int_rdiv(__attr __args[]); +__attr __fn_native__int_rmod(__attr __args[]); +__attr __fn_native__int_rpow(__attr __args[]); +__attr __fn_native__int_lt(__attr __args[]); +__attr __fn_native__int_gt(__attr __args[]); +__attr __fn_native__int_eq(__attr __args[]); +__attr __fn_native__str_add(__attr __args[]); +__attr __fn_native__str_lt(__attr __args[]); +__attr __fn_native__str_gt(__attr __args[]); +__attr __fn_native__str_eq(__attr __args[]); +__attr __fn_native__str_len(__attr __args[]); +__attr __fn_native__str_nonempty(__attr __args[]); +__attr __fn_native__list_init(__attr __args[]); +__attr __fn_native__list_len(__attr __args[]); +__attr __fn_native__list_nonempty(__attr __args[]); +__attr __fn_native__list_element(__attr __args[]); +__attr __fn_native__list_to_tuple(__attr __args[]); +__attr __fn_native__tuple_init(__attr __args[]); +__attr __fn_native__tuple_len(__attr __args[]); +__attr __fn_native__tuple_element(__attr __args[]); +__attr __fn_native__isinstance(__attr __args[]); +void __main_native(); + +#endif /* __NATIVE_H__ */ diff -r 6a86d0599fd0 -r 49376f3ab03d translator.py --- a/translator.py Mon Oct 31 21:50:41 2016 +0100 +++ b/translator.py Mon Oct 31 21:53:38 2016 +0100 @@ -45,8 +45,9 @@ self.check_output() for module in self.importer.modules.values(): - tm = TranslatedModule(module.name, self.importer, self.deducer, self.optimiser) - tm.translate(module.filename, join(output, "%s.c" % module.name)) + if module.name != "native": + tm = TranslatedModule(module.name, self.importer, self.deducer, self.optimiser) + tm.translate(module.filename, join(output, "%s.c" % module.name)) # Classes representing intermediate translation results.