Commit Graph

308 Commits

Author SHA1 Message Date
bcc9f29ff5 Merge pull request 'feature/char-support' (#3) from feature/char-support into master
Reviewed-on: #3
2023-03-24 17:03:23 +01:00
259ac49981 ContextAnalysis: Make comparing more generous
Previously we only allowed numeric types to be compared using >,<,<= and >=. Now we allow all primitives types to be compared using the operators mentioned above.

It is now also possible to compare and assign ints and chars to each other.
2023-03-24 00:17:38 +01:00
2aff9b3d0d Char: Add support for chars.
First try at implementing chars. There are some problems with the grammar where I was not able to separate the single quotes from the captured char, but I managed to hack it together in the ContextAnalysis.

It's currently not possible to do any 'math' with chars, but I will give it a try soon.
2023-03-23 23:04:09 +01:00
a7e93f4f01 GenASM: Set the bytesToClearFromStack variable earlier
If you set the variable after visiting the function body all children will see bytesToClearFromStack == 0.
2023-03-23 22:52:52 +01:00
5f0e84198a GenASM+Void: Generate an implicit return
Void functions are allowed to be completely free of return statements. The problem is that this does not work on its own in assembler. We have to generate a return statement at the end of the function body if there is no return statement already.
2023-03-23 22:49:36 +01:00
aef2c84fdc Void Type: Fix a problem where calls to void functions were ignored
The ContextAnalysis visitor for braced blocks was not updated during void support implementation which is why I do it now. Although we are re-using the functionCall production rule a different visitor is required for visiting function calls directly inside braced blocks. This is because the other place where functionCalls are used the functionCall is annotated with a label with wraps the functionCall context inside a label context. Fortunately this is in fact simple wrapping so we just implement a visitor for the wrapping class that unwraps the functionCall and passes it to the visitor that actually implements the functionCall.
2023-03-23 21:49:59 +01:00
4219f93021 Merge pull request 'Add support for void types' (#2) from 31-void-type into master
Reviewed-on: #2
2023-03-23 14:28:24 +01:00
7965c89a60 VoidType: Add tests and fix some bugs 2023-03-23 13:10:20 +01:00
26eff47057 Fix typos. 2023-03-23 13:09:52 +01:00
Marvin Kaiser
53976615e1 31: Add void type 2023-03-23 12:47:22 +01:00
c124587983 git: ignore idea designer file 2023-03-23 03:35:11 +01:00
07e5a338a4 GenASM: Fix segfaults when calling malloc
This was a tricky one. In order to call malloc the stack needs to be 16 byte aligned. GenASM up until this point was 8 byte aligned. This commit changes the code generation so that it is guaranteed that the stack is 16 byte aligned before visiting a child node. We need to do this before visiting *any* child node because we can not know whether somewhere downstream some child node calls malloc.

To make it possible for a visitor method to determine if it needs to align the stack it has to assume that the stack is currently aligned. This is the other reason we ensure that the stack is aligned before visiting any child node.

On top of that some visitor methods did not clean up after themselves. I am not sure why this did not result in segfaults already, but I changed the code so that each visitor method leaves the stack in the same state after completion.
2023-03-23 03:21:10 +01:00
76419d86bb GenASM: Remove a few warnings 2023-03-23 02:59:01 +01:00
f55f2661de Tests: Fix failing test because the error message changed. 2023-03-23 00:32:50 +01:00
06609ae899 GenASM: Get number of local variables from the "localVariables" attribute of FunctionDefinition. 2023-03-22 23:40:02 +01:00
c5c01041e4 ContextAnalysis: Track local variables and function parameters separately.
This enables us to add all local variable Definitions to the DAST so that downstream visitors don't need to compute the local variables of a function again.
2023-03-22 23:37:43 +01:00
9751a1da2f Eval: Add the corresponding named type to a struct Value. 2023-03-22 00:14:24 +01:00
534b507f7a Eval: Make a NullExpression evaluate to a Value with Type "NulLValue" instead of returning null. 2023-03-22 00:13:23 +01:00
cce58b6e38 ContextAnalysis: Make naught comparable to struct again. 2023-03-21 22:44:21 +01:00
8b17ced533 Build: Set Java to verison 17 2023-03-21 22:43:40 +01:00
bacc40d844 GenASM: Make sure the stack is 16 byte aligned before calling malloc or free.
The GenASM visitor only uses quad mnemonics so each manipulation to the stack is done in 8 byte chunks. This means our stack is always 8 byte aligned which is fine when only calling KLang code. libc functions like 'malloc' or 'free' require the stack to be 16 byte aligned which isn't automatically guaranteed when being 8 byte aligned. This is why we need to be careful when manipulating the stack (e.g. pushq, popq, subq x, %rsp). By assuming that we are stack aligned at the beginning of execution and by tracking the number of pushq operations we can deduce whether we misaligned the stack and align it again by adding another 8 byte to the stack.

This re-alignment of the stack is done directly before entering a function body or calling malloc. Since there are no stack manipulations involved when generating the code to calling 'free' there is reason to check for alignment there.
2023-03-21 22:21:14 +01:00
f38bd3d69e GenASM: Use leave instead of mov and pop for returning from a function. 2023-03-21 00:22:11 +01:00
441d0122f8 Merge pull request 'feature/add-enum-support' (#1) from feature/add-enum-support into master
Reviewed-on: #1
2023-03-20 21:19:53 +01:00
e835bd0f06 GenASM: Make GenASM quietly rewrite a user's function if it's called main.
We generate our own main function that executes the user's specified expression at the end of his file. This auto generated function has to be called "main" in order for it to be executed on startup. If the user chooses to call one of his own functions "main" our auto generated function would collide with the user's function. That's why we quietly rewrite the user's function to "main_by_user". This way we prevent a collision.

Note for the future: We should change our Syntax to allow for no expression as the last definition of a file. This way the user can choose if a particular source file needs to contain a main function or not (just like c does it). This is also one of the requirements for modules to work.
2023-03-20 21:05:24 +01:00
ea1c04ae0a Build: Add a main manifest attribute to the generated jar.
This makes it possible to directly execute the packaged jar.
2023-03-20 19:55:37 +01:00
198bd74a47 Enums: Make the EnumAccessExpression save a reference to the EnumValue it is referencing.
This can be used during assembler generation to easily find the correct EnumValue for a given EnumAccessExpression.
2023-03-20 19:54:48 +01:00
0594542167 Enums: Make EnumDefinition use EnumValues instead of Strings as children.
This allows us to store the index of the enum value along the name. The index can be used to compare two enum values in assembler.

Later on this might be used to enable users of KLang to set arbitrary values as the index of an enum value.
2023-03-20 19:30:07 +01:00
77fe360ffa Evaluate: Implement evaluation for enums. 2023-03-20 19:10:40 +01:00
55a5b8f54a Make sure that a variable that references an enum has to be initialized. 2023-03-16 00:01:31 +01:00
2768b4429c Check that a struct field name of a struct declaration does not shadow an enum or a struct definition. 2023-03-15 23:48:57 +01:00
30dfbbbbba Check that a variable name of variable declaration does not shadow an enum definition. 2023-03-15 23:33:12 +01:00
f77d6a002d Check that a parameter name of a function definition does not shadow an enum definition. 2023-03-15 23:17:43 +01:00
22634c9652 Use LinkedHashMaps and LinkedHashSets to preserve the order of parameters and struct fields. 2023-03-15 23:08:38 +01:00
6fd3f5a2e6 Make it possible to use an enum in an expression (i.e. selecting one of the enum values: Foo.A) 2023-03-15 19:14:04 +01:00
3b928d621b Refactor FunctionDefinition and Parameter context analysis and extend the type check to include enums. 2023-03-15 17:47:58 +01:00
9a58afb550 Implement StructField type checking in ContextAnalysis. 2023-03-15 17:21:35 +01:00
6e4431652c Remove FunctionInformation and replace it with FunctionDefinition. 2023-03-15 16:19:42 +01:00
7af815042b WIP: Add enum support 2023-03-15 15:56:42 +01:00
7c40a50196 add intellij config files 2023-03-15 05:23:09 +01:00
Dennis Kaiser
8529e24a37 Merge branch '32-create-asm-class-structure' into 'master'
32: Create Meta ASM Structure

Closes #32

See merge request mkais001/klang!23
2020-03-17 16:08:54 +01:00
Marvin Kaiser
49b024b95f 32: Require data type for all asm functions 2020-03-17 16:02:03 +01:00
Marvin Kaiser
982fc6417d 32: Create Meta ASM Structure 2020-03-14 14:14:19 +01:00
Dennis Kaiser
bd173b1d45 Merge branch 'bug/fix-tco' into 'master'
push all args onto stack before moving them into the local var to ensure that...

See merge request mkais001/klang!22
abgabe_compilerbau
2020-03-10 12:19:34 +01:00
221b928d0e push all args onto stack before moving them into the local var to ensure that the function parameters can be used in the tail recursive function call 2020-03-10 12:07:55 +01:00
Marvin Kaiser
8dd0b6cffa Fix pretty printing to file 2020-03-10 11:21:49 +01:00
Marvin Kaiser
f288d5585f Update README.md 2020-03-10 11:04:03 +01:00
Dennis Kaiser
e05ca07d23 Merge branch 'extend-readme' into 'master'
add section explaining structs

See merge request mkais001/klang!21
2020-03-09 23:27:45 +01:00
fd17a25f29 add section explaining structs 2020-03-09 23:18:46 +01:00
Dennis Kaiser
500cfaffbe Merge branch 'fix-do-while' into 'master'
visit block first because the condition variable may be initialized inside the block

See merge request mkais001/klang!20
2020-03-09 23:16:33 +01:00
5a5191612e visit block first because the condition variable may be initialized inside the block 2020-03-09 23:04:55 +01:00