Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow top-level lets and introduce const declarations. #278

Merged
merged 5 commits into from
Sep 29, 2021

Conversation

otrho
Copy link
Contributor

@otrho otrho commented Sep 29, 2021

This turned out to be bigger than I thought it would. Closes #215.

For now const declarations can only be initialised with a literal. In the future we may want to allow any const expression.

I had to touch a bunch of stuff, right from the type checker, the control flow analyser, and asm gen and I'm fairly confident it's right, but much of it was new to me so I might've missed something.

In particular I've added some code to the asm gen which adds ops for the constants and their initialisers before it converts main. This is because const can be a global, but unlike functions, enums or structs, they do need asm to be generated, and since globals aren't in functions they won't be instantiated when main is.

@otrho otrho force-pushed the otrho/215_top_level_decls branch from a2425f5 to c4e1b45 Compare September 29, 2021 14:24
@otrho otrho self-assigned this Sep 29, 2021
@otrho otrho changed the title Disallow top-level lets and introduct const declarations. Disallow top-level lets and introduce const declarations. Sep 29, 2021
@sezna
Copy link
Contributor

sezna commented Sep 29, 2021

This looks fine and makes sense to me, but I have a question. I was thinking we could just parse them (only allowing literals, the same as your intuition) and then just pre-populate the data section and namespace with them. Then whenever they were referenced, they'd already have a data section entry and it would all fall into place.

Is what I just described too naïve?

@otrho
Copy link
Contributor Author

otrho commented Sep 29, 2021

I don't know -- how is that different to what I've done? My mental model of how it works still isn't 100%, I think.

Everything pre-asm would have to stay, yeah? It needs to type-check and pass dead code analysis. And then in the asm-gen I'm just putting the values into registers and and the namespace. That sounds different to 'pre-populating the data section' but I don't quite understand the difference, or whether it would be any simpler/better..?

@otrho
Copy link
Contributor Author

otrho commented Sep 29, 2021

Hrmm, what it does now is add each const to the namespace by using convert_expression_to_asm() which in turn at this stage will always use convert_literal_to_asm() which puts the literal into the data section in the namespace and creates an LW instruction.

I see that each module type (script, predicate, etc.) has their own data section though, is that what you mean?

Otherwise, to pre-populate the namespace you need to associate a register with a name, and to get a register you need to generate the load instruction...

@sezna
Copy link
Contributor

sezna commented Sep 29, 2021

You don't need a register to reference a data section value, instead you get a data label. But I see what you mean, you then need to get that data label associated with the variable name somehow....

Ok, this makes sense. And it allows us to do more complicated constant expressions in the future. Feel free to resolve the conflicts and we can take this in.

@otrho otrho force-pushed the otrho/215_top_level_decls branch from c4e1b45 to 2c0f4df Compare September 29, 2021 21:56
@otrho
Copy link
Contributor Author

otrho commented Sep 29, 2021

Just did a forced push to resolve merge conflicts.

@otrho otrho merged commit 1f66ee9 into master Sep 29, 2021
@otrho otrho deleted the otrho/215_top_level_decls branch September 29, 2021 22:15
@adlerjohn adlerjohn added the compiler General compiler. Should eventually become more specific as the issue is triaged label Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Either support or disallow top level variable declarations.
3 participants