Skip to content

Commit

Permalink
Allocate structs on the heap, not on the stack, in C backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
cpressey committed Jun 30, 2021
1 parent 0183881 commit 50bdf20
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
19 changes: 14 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
TODO
----

Don't output final value. Command-line arguments passed to `main`. (`sysmain`?)

Name mangling for compilers (prepend with `_` most likely.)
### Tests ###

Tests for unions of unions.

Test for equality of union values.

Tests for multiple occurrences of same type in a union.

Test for casting a union to the same union or a different union.

### Implementation ###

Name mangling for compilers (prepend with `_` most likely.)

Struct equality in Javascript, stackmac backends.

Figure out a way to do `input`, `read`, and `write` with node.js backend.

Implement `int`, `str`, `chr`, `ord` for Ruby, Javascript, stackmac.
Implement `int`, `chr`, `ord` for Ruby, Javascript, stackmac.

TaggedValue -> just a tuple.

Expand All @@ -27,10 +29,17 @@ and void types in unions of (void, X) should only be one value.

AST nodes should have source line numbers, it would be really nice.

C backend. Other backends (Python? Java? CIL? Scheme?)
Finish C backend.

Implement garbage collection of some sort in the C backend. Either that
or implement some kind of resource-awareness in the language itself.

Other backends (Python? Java? CIL? Scheme?)

### Design ###

Don't output final value. Command-line arguments passed to `main`. (`sysmain`?)

Convenience:

* Should we have automatic promotion (value tagging?)
Expand Down
24 changes: 22 additions & 2 deletions src/castile/backends/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,26 @@ def compile(self, ast):
self.compile(child)
self.indent -= 1
self.write_indent('};\n\n')
self.write_indent('struct %s * make_%s(' % (ast.value, ast.value))

for child in ast.children[:-1]:
assert child.tag == 'FieldDefn'
self.write('%s, ' % self.c_decl(child.children[0].type, child.value))
child = ast.children[-1]
assert child.tag == 'FieldDefn'
self.write('%s' % self.c_decl(child.children[0].type, child.value))

self.write(') {\n')
self.indent += 1
self.write_indent('struct %s *x = malloc(sizeof(struct %s));\n' % (ast.value, ast.value))

for child in ast.children:
assert child.tag == 'FieldDefn'
self.write_indent('x->%s = %s;\n' % (child.value, child.value))

self.write_indent('return x;\n')
self.indent -= 1
self.write_indent('}\n')
elif ast.tag == 'FieldDefn':
self.write_indent('%s;\n' % self.c_decl(ast.children[0].type, ast.value))
elif ast.tag == 'FunLit':
Expand Down Expand Up @@ -283,7 +303,7 @@ def compile(self, ast):
self.write(' = ')
self.compile(ast.children[1])
elif ast.tag == 'Make':
self.write('&(struct %s){ ' % ast.type.name)
self.write('make_%s(' % ast.type.name)
def find_field(name):
for field in ast.children[1:]:
if field.value == name:
Expand All @@ -292,7 +312,7 @@ def find_field(name):
for field_name in ast.type.defn.field_names_in_order():
ordered_fields.append(find_field(field_name))
self.commas(ordered_fields)
self.write(' }')
self.write(')')
elif ast.tag == 'FieldInit':
self.commas(ast.children)
elif ast.tag == 'Index':
Expand Down

0 comments on commit 50bdf20

Please sign in to comment.