gofra 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gofra-0.0.1/LICENSE +21 -0
- gofra-0.0.1/PKG-INFO +107 -0
- gofra-0.0.1/README.md +93 -0
- gofra-0.0.1/gofra/__init__.py +12 -0
- gofra-0.0.1/gofra/__main__.py +6 -0
- gofra-0.0.1/gofra/assembler/__init__.py +8 -0
- gofra-0.0.1/gofra/assembler/assembler.py +239 -0
- gofra-0.0.1/gofra/assembler/exceptions.py +19 -0
- gofra-0.0.1/gofra/cli/__init__.py +6 -0
- gofra-0.0.1/gofra/cli/arguments.py +294 -0
- gofra-0.0.1/gofra/cli/entry_point.py +122 -0
- gofra-0.0.1/gofra/cli/errors.py +20 -0
- gofra-0.0.1/gofra/cli/ir.py +65 -0
- gofra-0.0.1/gofra/cli/output.py +29 -0
- gofra-0.0.1/gofra/codegen/__init__.py +8 -0
- gofra-0.0.1/gofra/codegen/backends/__init__.py +14 -0
- gofra-0.0.1/gofra/codegen/backends/aarch64_macos/__init__.py +5 -0
- gofra-0.0.1/gofra/codegen/backends/aarch64_macos/_context.py +23 -0
- gofra-0.0.1/gofra/codegen/backends/aarch64_macos/assembly.py +291 -0
- gofra-0.0.1/gofra/codegen/backends/aarch64_macos/codegen.py +264 -0
- gofra-0.0.1/gofra/codegen/backends/aarch64_macos/registers.py +50 -0
- gofra-0.0.1/gofra/codegen/backends/amd64_linux/__init__.py +5 -0
- gofra-0.0.1/gofra/codegen/backends/amd64_linux/_context.py +23 -0
- gofra-0.0.1/gofra/codegen/backends/amd64_linux/assembly.py +255 -0
- gofra-0.0.1/gofra/codegen/backends/amd64_linux/codegen.py +262 -0
- gofra-0.0.1/gofra/codegen/backends/amd64_linux/registers.py +50 -0
- gofra-0.0.1/gofra/codegen/backends/base.py +16 -0
- gofra-0.0.1/gofra/codegen/backends/general.py +43 -0
- gofra-0.0.1/gofra/codegen/exceptions.py +19 -0
- gofra-0.0.1/gofra/codegen/generator.py +25 -0
- gofra-0.0.1/gofra/codegen/get_backend.py +21 -0
- gofra-0.0.1/gofra/codegen/targets.py +3 -0
- gofra-0.0.1/gofra/consts.py +3 -0
- gofra-0.0.1/gofra/context.py +33 -0
- gofra-0.0.1/gofra/exceptions.py +9 -0
- gofra-0.0.1/gofra/gofra.py +26 -0
- gofra-0.0.1/gofra/lexer/__init__.py +20 -0
- gofra-0.0.1/gofra/lexer/_state.py +23 -0
- gofra-0.0.1/gofra/lexer/exceptions.py +94 -0
- gofra-0.0.1/gofra/lexer/helpers.py +67 -0
- gofra-0.0.1/gofra/lexer/io/__init__.py +8 -0
- gofra-0.0.1/gofra/lexer/io/exceptions.py +32 -0
- gofra-0.0.1/gofra/lexer/io/io.py +37 -0
- gofra-0.0.1/gofra/lexer/keywords.py +49 -0
- gofra-0.0.1/gofra/lexer/lexer.py +192 -0
- gofra-0.0.1/gofra/lexer/tokens.py +54 -0
- gofra-0.0.1/gofra/optimizer/__init__.py +5 -0
- gofra-0.0.1/gofra/optimizer/optimizer.py +12 -0
- gofra-0.0.1/gofra/optimizer/strategies/__init__.py +6 -0
- gofra-0.0.1/gofra/optimizer/strategies/constant_folding.py +198 -0
- gofra-0.0.1/gofra/optimizer/strategies/dead_code_elimination.py +38 -0
- gofra-0.0.1/gofra/parser/__init__.py +6 -0
- gofra-0.0.1/gofra/parser/_context.py +99 -0
- gofra-0.0.1/gofra/parser/exceptions.py +257 -0
- gofra-0.0.1/gofra/parser/functions/__init__.py +3 -0
- gofra-0.0.1/gofra/parser/functions/exceptions.py +92 -0
- gofra-0.0.1/gofra/parser/functions/function.py +114 -0
- gofra-0.0.1/gofra/parser/functions/parser.py +219 -0
- gofra-0.0.1/gofra/parser/intrinsics.py +63 -0
- gofra-0.0.1/gofra/parser/operators.py +63 -0
- gofra-0.0.1/gofra/parser/parser.py +419 -0
- gofra-0.0.1/gofra/parser/validator.py +36 -0
- gofra-0.0.1/gofra/preprocessor/__init__.py +8 -0
- gofra-0.0.1/gofra/preprocessor/_state.py +56 -0
- gofra-0.0.1/gofra/preprocessor/conditions/__init__.py +51 -0
- gofra-0.0.1/gofra/preprocessor/conditions/exceptions.py +25 -0
- gofra-0.0.1/gofra/preprocessor/exceptions.py +14 -0
- gofra-0.0.1/gofra/preprocessor/include/__init__.py +3 -0
- gofra-0.0.1/gofra/preprocessor/include/exceptions.py +60 -0
- gofra-0.0.1/gofra/preprocessor/include/resolver.py +91 -0
- gofra-0.0.1/gofra/preprocessor/macros/__init__.py +6 -0
- gofra-0.0.1/gofra/preprocessor/macros/container.py +16 -0
- gofra-0.0.1/gofra/preprocessor/macros/exceptions.py +80 -0
- gofra-0.0.1/gofra/preprocessor/macros/preprocessor.py +108 -0
- gofra-0.0.1/gofra/preprocessor/preprocessor.py +52 -0
- gofra-0.0.1/gofra/testkit/__init__.py +0 -0
- gofra-0.0.1/gofra/testkit/__main__.py +4 -0
- gofra-0.0.1/gofra/testkit/arguments.py +48 -0
- gofra-0.0.1/gofra/testkit/entry_point.py +107 -0
- gofra-0.0.1/gofra/typecheck/__init__.py +8 -0
- gofra-0.0.1/gofra/typecheck/_context.py +100 -0
- gofra-0.0.1/gofra/typecheck/exceptions.py +200 -0
- gofra-0.0.1/gofra/typecheck/typechecker.py +263 -0
- gofra-0.0.1/gofra/typecheck/types.py +31 -0
- gofra-0.0.1/pyproject.toml +44 -0
gofra-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Kirill Zhosul
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
gofra-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: gofra
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A Stack-based compiled programming language
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: Kirill Zhosul
|
|
7
|
+
Author-email: kirillzhosul@yandex.com
|
|
8
|
+
Requires-Python: >=3.12,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# Gofra Programming Language
|
|
16
|
+
|
|
17
|
+
Gofra is an native stack based programming language.
|
|
18
|
+
|
|
19
|
+
Language follows reverse polish notation (examples can be found below) \
|
|
20
|
+
**Language is in development stage and mostly made for fun and research so don't expect a lot and try to test or implement your own idea**
|
|
21
|
+
|
|
22
|
+
## Table of content
|
|
23
|
+
|
|
24
|
+
- [Hello world example](#hello-world-example)
|
|
25
|
+
- [Compatibility](#compatibility)
|
|
26
|
+
- [Features](#features)
|
|
27
|
+
- [Installation](#installation)
|
|
28
|
+
- [Examples](#examples)
|
|
29
|
+
- [Language overview](#language-overview)
|
|
30
|
+
- [Command Line Interface](#command-line-interface-cli)
|
|
31
|
+
- [Milestones and planned features](#milestones-and-planned-features)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
### Hello world example
|
|
36
|
+
###### (For now, language is mostly bare-metal so in this example there is raw `sc_write` syscall and file descriptor usage)
|
|
37
|
+
```
|
|
38
|
+
include "std.gof"
|
|
39
|
+
func void main
|
|
40
|
+
FD_STD_OUT "Hello, World!\n" sc_write drop
|
|
41
|
+
end
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
### Compatibility
|
|
46
|
+
Language currently have codegenS only for:
|
|
47
|
+
- AARCH64 MacOS (Darwin)
|
|
48
|
+
- x86_64 Linux
|
|
49
|
+
|
|
50
|
+
### Features
|
|
51
|
+
- Native (codegen assembly)
|
|
52
|
+
- Type safety (Validates stack usage and tries to infer types so you wont mess up)
|
|
53
|
+
- Mostly self explanation errors (Tries to help you and correct your intentions)
|
|
54
|
+
- Optimizer (DCE, CF, Helps optimize resulting assembly for codegen so your default usage will not be overwhelmed by language)
|
|
55
|
+
- FFI with `global`/`extern` function modifers (there is CLI flags to emit an library/object file)
|
|
56
|
+
- Simple CLI for working with language (simple toolkit)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
### Installation
|
|
60
|
+
- Clone this repo
|
|
61
|
+
- Install latest Python version
|
|
62
|
+
- Navigate to root directory
|
|
63
|
+
- Run `python -m gofra --help` (`python` depends on your installation of Python)
|
|
64
|
+
|
|
65
|
+
### Examples
|
|
66
|
+
Examples may be found inside `./examples` directory
|
|
67
|
+
|
|
68
|
+
### Language overview
|
|
69
|
+
As language is stack based so your basic action is to *put something on a stack*, like `2 2` will push 2 and then another 2 on stack so stack underneath will look like [2, 2]
|
|
70
|
+
|
|
71
|
+
If you want to operate on that numbers you may do something like `3 2 +` which is same as `3 + 2` in other language or default math. Underneath this will mean: push 3 on stack -> push 2 on stack -> take 2 elements from stack -> sum them -> push result back. Stack after that will become [5]
|
|
72
|
+
|
|
73
|
+
Conditionals is also a bit controversial:
|
|
74
|
+
```
|
|
75
|
+
1 2 == if
|
|
76
|
+
...
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
which is same as other languages:
|
|
80
|
+
```
|
|
81
|
+
if (1 == 2){
|
|
82
|
+
...
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
(You can follow previous math example for checking stack manipulation)
|
|
86
|
+
|
|
87
|
+
For writing a bit more complex programs you may want to use macros and includes:
|
|
88
|
+
Macros is an collection of tokens (like functions in other languages) but does not have an object-like system they just an way to not write same logic (for now)
|
|
89
|
+
So, this code:
|
|
90
|
+
```
|
|
91
|
+
macro multiply_by_2
|
|
92
|
+
2 *
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
4 multiply_by_2
|
|
96
|
+
```
|
|
97
|
+
at compilation stage will be converted into simple `4 2 *` (tokens expanded)
|
|
98
|
+
For importing some file (same as macros system but for files) you can use `import "file.gof"`
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
### Milestones and planned features
|
|
103
|
+
|
|
104
|
+
- Standard library with not only syscall mapping
|
|
105
|
+
- Stability improvements
|
|
106
|
+
- Support for x86_64 Windows
|
|
107
|
+
- More examples
|
gofra-0.0.1/README.md
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# Gofra Programming Language
|
|
2
|
+
|
|
3
|
+
Gofra is an native stack based programming language.
|
|
4
|
+
|
|
5
|
+
Language follows reverse polish notation (examples can be found below) \
|
|
6
|
+
**Language is in development stage and mostly made for fun and research so don't expect a lot and try to test or implement your own idea**
|
|
7
|
+
|
|
8
|
+
## Table of content
|
|
9
|
+
|
|
10
|
+
- [Hello world example](#hello-world-example)
|
|
11
|
+
- [Compatibility](#compatibility)
|
|
12
|
+
- [Features](#features)
|
|
13
|
+
- [Installation](#installation)
|
|
14
|
+
- [Examples](#examples)
|
|
15
|
+
- [Language overview](#language-overview)
|
|
16
|
+
- [Command Line Interface](#command-line-interface-cli)
|
|
17
|
+
- [Milestones and planned features](#milestones-and-planned-features)
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
### Hello world example
|
|
22
|
+
###### (For now, language is mostly bare-metal so in this example there is raw `sc_write` syscall and file descriptor usage)
|
|
23
|
+
```
|
|
24
|
+
include "std.gof"
|
|
25
|
+
func void main
|
|
26
|
+
FD_STD_OUT "Hello, World!\n" sc_write drop
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Compatibility
|
|
32
|
+
Language currently have codegenS only for:
|
|
33
|
+
- AARCH64 MacOS (Darwin)
|
|
34
|
+
- x86_64 Linux
|
|
35
|
+
|
|
36
|
+
### Features
|
|
37
|
+
- Native (codegen assembly)
|
|
38
|
+
- Type safety (Validates stack usage and tries to infer types so you wont mess up)
|
|
39
|
+
- Mostly self explanation errors (Tries to help you and correct your intentions)
|
|
40
|
+
- Optimizer (DCE, CF, Helps optimize resulting assembly for codegen so your default usage will not be overwhelmed by language)
|
|
41
|
+
- FFI with `global`/`extern` function modifers (there is CLI flags to emit an library/object file)
|
|
42
|
+
- Simple CLI for working with language (simple toolkit)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
### Installation
|
|
46
|
+
- Clone this repo
|
|
47
|
+
- Install latest Python version
|
|
48
|
+
- Navigate to root directory
|
|
49
|
+
- Run `python -m gofra --help` (`python` depends on your installation of Python)
|
|
50
|
+
|
|
51
|
+
### Examples
|
|
52
|
+
Examples may be found inside `./examples` directory
|
|
53
|
+
|
|
54
|
+
### Language overview
|
|
55
|
+
As language is stack based so your basic action is to *put something on a stack*, like `2 2` will push 2 and then another 2 on stack so stack underneath will look like [2, 2]
|
|
56
|
+
|
|
57
|
+
If you want to operate on that numbers you may do something like `3 2 +` which is same as `3 + 2` in other language or default math. Underneath this will mean: push 3 on stack -> push 2 on stack -> take 2 elements from stack -> sum them -> push result back. Stack after that will become [5]
|
|
58
|
+
|
|
59
|
+
Conditionals is also a bit controversial:
|
|
60
|
+
```
|
|
61
|
+
1 2 == if
|
|
62
|
+
...
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
which is same as other languages:
|
|
66
|
+
```
|
|
67
|
+
if (1 == 2){
|
|
68
|
+
...
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
(You can follow previous math example for checking stack manipulation)
|
|
72
|
+
|
|
73
|
+
For writing a bit more complex programs you may want to use macros and includes:
|
|
74
|
+
Macros is an collection of tokens (like functions in other languages) but does not have an object-like system they just an way to not write same logic (for now)
|
|
75
|
+
So, this code:
|
|
76
|
+
```
|
|
77
|
+
macro multiply_by_2
|
|
78
|
+
2 *
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
4 multiply_by_2
|
|
82
|
+
```
|
|
83
|
+
at compilation stage will be converted into simple `4 2 *` (tokens expanded)
|
|
84
|
+
For importing some file (same as macros system but for files) you can use `import "file.gof"`
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
### Milestones and planned features
|
|
89
|
+
|
|
90
|
+
- Standard library with not only syscall mapping
|
|
91
|
+
- Stability improvements
|
|
92
|
+
- Support for x86_64 Windows
|
|
93
|
+
- More examples
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"""Assembler module to assemble programs in Gofra language into executables."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from platform import system as current_platform_system
|
|
8
|
+
from shutil import which
|
|
9
|
+
from subprocess import CalledProcessError, check_output
|
|
10
|
+
from typing import TYPE_CHECKING, Literal
|
|
11
|
+
|
|
12
|
+
from gofra.cli.output import cli_message
|
|
13
|
+
from gofra.codegen import generate_code_for_assembler
|
|
14
|
+
from gofra.codegen.backends.general import CODEGEN_ENTRY_POINT_SYMBOL
|
|
15
|
+
from gofra.codegen.get_backend import get_backend_for_target
|
|
16
|
+
|
|
17
|
+
from .exceptions import (
|
|
18
|
+
NoToolkitForAssemblingError,
|
|
19
|
+
UnsupportedBuilderOperatingSystemError,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
if TYPE_CHECKING:
|
|
23
|
+
from gofra.codegen.targets import TARGET_T
|
|
24
|
+
from gofra.context import ProgramContext
|
|
25
|
+
|
|
26
|
+
type OUTPUT_FORMAT_T = Literal["library", "executable", "object", "assembly"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def assemble_program( # noqa: PLR0913
|
|
30
|
+
context: ProgramContext,
|
|
31
|
+
output: Path,
|
|
32
|
+
output_format: OUTPUT_FORMAT_T,
|
|
33
|
+
target: TARGET_T,
|
|
34
|
+
*,
|
|
35
|
+
build_cache_dir: Path,
|
|
36
|
+
verbose: bool,
|
|
37
|
+
additional_linker_flags: list[str],
|
|
38
|
+
additional_assembler_flags: list[str],
|
|
39
|
+
delete_build_cache_after_compilation: bool,
|
|
40
|
+
) -> None:
|
|
41
|
+
"""Convert given program into executable/library/etc using assembly and linker."""
|
|
42
|
+
_validate_toolkit_installation()
|
|
43
|
+
_prepare_build_cache_directory(build_cache_dir)
|
|
44
|
+
|
|
45
|
+
assembly_filepath = _generate_assembly_file_with_codegen(
|
|
46
|
+
context,
|
|
47
|
+
target,
|
|
48
|
+
output,
|
|
49
|
+
build_cache_dir=build_cache_dir,
|
|
50
|
+
verbose=verbose,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if output_format == "assembly":
|
|
54
|
+
assembly_filepath.replace(output)
|
|
55
|
+
return
|
|
56
|
+
|
|
57
|
+
object_filepath = _assemble_object_file(
|
|
58
|
+
target,
|
|
59
|
+
assembly_filepath,
|
|
60
|
+
output,
|
|
61
|
+
additional_assembler_flags=additional_assembler_flags,
|
|
62
|
+
build_cache_dir=build_cache_dir,
|
|
63
|
+
verbose=verbose,
|
|
64
|
+
)
|
|
65
|
+
if output_format == "object":
|
|
66
|
+
object_filepath.replace(output)
|
|
67
|
+
if delete_build_cache_after_compilation:
|
|
68
|
+
assembly_filepath.unlink()
|
|
69
|
+
return
|
|
70
|
+
|
|
71
|
+
assert output_format in ("executable", "library")
|
|
72
|
+
_link_final_output(
|
|
73
|
+
output,
|
|
74
|
+
target,
|
|
75
|
+
object_filepath,
|
|
76
|
+
output_format=output_format,
|
|
77
|
+
additional_linker_flags=additional_linker_flags,
|
|
78
|
+
verbose=verbose,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
if delete_build_cache_after_compilation:
|
|
82
|
+
assembly_filepath.unlink()
|
|
83
|
+
object_filepath.unlink()
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _prepare_build_cache_directory(build_cache_directory: Path) -> None:
|
|
87
|
+
"""Try to create and fill cache directory with required files."""
|
|
88
|
+
if build_cache_directory.exists():
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
build_cache_directory.mkdir(exist_ok=False)
|
|
92
|
+
|
|
93
|
+
with (build_cache_directory / ".gitignore").open("w") as f:
|
|
94
|
+
f.write("# Do not include this newly generated build cache into git VCS\n")
|
|
95
|
+
f.write("*\n")
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _link_final_output( # noqa: PLR0913
|
|
99
|
+
output: Path,
|
|
100
|
+
target: TARGET_T,
|
|
101
|
+
o_filepath: Path,
|
|
102
|
+
output_format: Literal["executable", "library"],
|
|
103
|
+
additional_linker_flags: list[str],
|
|
104
|
+
*,
|
|
105
|
+
verbose: bool,
|
|
106
|
+
) -> None:
|
|
107
|
+
"""Use linker to link object file into executable."""
|
|
108
|
+
match current_platform_system():
|
|
109
|
+
case "Darwin":
|
|
110
|
+
assert target == "aarch64-darwin"
|
|
111
|
+
|
|
112
|
+
system_sdk = Path(
|
|
113
|
+
check_output( # noqa: S603
|
|
114
|
+
["/usr/bin/xcrun", "-sdk", "macosx", "--show-sdk-path"],
|
|
115
|
+
text=True,
|
|
116
|
+
).strip(),
|
|
117
|
+
)
|
|
118
|
+
target_linker_flags = [
|
|
119
|
+
"-arch",
|
|
120
|
+
"arm64",
|
|
121
|
+
"-lSystem",
|
|
122
|
+
"-syslibroot",
|
|
123
|
+
str(system_sdk),
|
|
124
|
+
]
|
|
125
|
+
|
|
126
|
+
if output_format == "library":
|
|
127
|
+
target_linker_flags += ["-dylib"]
|
|
128
|
+
case "Linux":
|
|
129
|
+
assert target == "x86_64-linux"
|
|
130
|
+
|
|
131
|
+
target_linker_flags = []
|
|
132
|
+
assert output_format == "executable", (
|
|
133
|
+
"Libraries on Linux is not implemented"
|
|
134
|
+
)
|
|
135
|
+
case _:
|
|
136
|
+
raise UnsupportedBuilderOperatingSystemError
|
|
137
|
+
|
|
138
|
+
linker_flags = [
|
|
139
|
+
*target_linker_flags,
|
|
140
|
+
*additional_linker_flags,
|
|
141
|
+
]
|
|
142
|
+
|
|
143
|
+
if output_format == "executable":
|
|
144
|
+
linker_flags += ["-e", CODEGEN_ENTRY_POINT_SYMBOL]
|
|
145
|
+
|
|
146
|
+
command = ["/usr/bin/ld", "-o", str(output), str(o_filepath), *linker_flags]
|
|
147
|
+
cli_message(
|
|
148
|
+
level="INFO",
|
|
149
|
+
text=f"Running linker command: `{' '.join(command)}`",
|
|
150
|
+
verbose=verbose,
|
|
151
|
+
)
|
|
152
|
+
check_output(command) # noqa: S603
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def _assemble_object_file( # noqa: PLR0913
|
|
156
|
+
target: TARGET_T,
|
|
157
|
+
asm_filepath: Path,
|
|
158
|
+
output: Path,
|
|
159
|
+
*,
|
|
160
|
+
build_cache_dir: Path,
|
|
161
|
+
additional_assembler_flags: list[str],
|
|
162
|
+
verbose: bool,
|
|
163
|
+
) -> Path:
|
|
164
|
+
"""Call assembler to assemble given assembly file from codegen."""
|
|
165
|
+
object_filepath = (build_cache_dir / output.name).with_suffix(".o")
|
|
166
|
+
|
|
167
|
+
# Assembler is not crossplatform so we expect host has same architecture
|
|
168
|
+
match current_platform_system():
|
|
169
|
+
case "Darwin":
|
|
170
|
+
if target != "aarch64-darwin":
|
|
171
|
+
raise UnsupportedBuilderOperatingSystemError
|
|
172
|
+
assembler_flags = ["-arch", "arm64"]
|
|
173
|
+
case "Linux":
|
|
174
|
+
if target != "x86_64-linux":
|
|
175
|
+
raise UnsupportedBuilderOperatingSystemError
|
|
176
|
+
assembler_flags = []
|
|
177
|
+
case _:
|
|
178
|
+
raise UnsupportedBuilderOperatingSystemError
|
|
179
|
+
|
|
180
|
+
command = [
|
|
181
|
+
"/usr/bin/as",
|
|
182
|
+
"-o",
|
|
183
|
+
str(object_filepath),
|
|
184
|
+
str(asm_filepath),
|
|
185
|
+
*assembler_flags,
|
|
186
|
+
*additional_assembler_flags,
|
|
187
|
+
]
|
|
188
|
+
cli_message(
|
|
189
|
+
level="INFO",
|
|
190
|
+
text=f"Running command: `{' '.join(command)}`",
|
|
191
|
+
verbose=verbose,
|
|
192
|
+
)
|
|
193
|
+
try:
|
|
194
|
+
check_output(command) # noqa: S603
|
|
195
|
+
except CalledProcessError as e:
|
|
196
|
+
cli_message(
|
|
197
|
+
"ERROR",
|
|
198
|
+
"Failed to generate binary from output assembly, "
|
|
199
|
+
f"error code: {e.returncode}",
|
|
200
|
+
)
|
|
201
|
+
sys.exit(1)
|
|
202
|
+
|
|
203
|
+
return object_filepath
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _generate_assembly_file_with_codegen(
|
|
207
|
+
context: ProgramContext,
|
|
208
|
+
target: TARGET_T,
|
|
209
|
+
output: Path,
|
|
210
|
+
*,
|
|
211
|
+
build_cache_dir: Path,
|
|
212
|
+
verbose: bool,
|
|
213
|
+
) -> Path:
|
|
214
|
+
"""Call desired codegen backend for requested target and generate file contains assembly."""
|
|
215
|
+
assembly_filepath = (build_cache_dir / output.name).with_suffix(".s")
|
|
216
|
+
|
|
217
|
+
infered_backend = get_backend_for_target(target).__name__ # type: ignore # noqa: PGH003
|
|
218
|
+
cli_message(
|
|
219
|
+
level="INFO",
|
|
220
|
+
text=f"Generating assembly using codegen backend (Infered codegen for target `{target}` is `{infered_backend}`)...",
|
|
221
|
+
verbose=verbose,
|
|
222
|
+
)
|
|
223
|
+
generate_code_for_assembler(assembly_filepath, context, target)
|
|
224
|
+
return assembly_filepath
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _validate_toolkit_installation() -> None:
|
|
228
|
+
"""Validate that the host system has all requirements installed (linker/assembler)."""
|
|
229
|
+
match current_platform_system():
|
|
230
|
+
case "Darwin":
|
|
231
|
+
required_toolkit = ("as", "ld", "xcrun")
|
|
232
|
+
case "Linux":
|
|
233
|
+
required_toolkit = ("as", "ld")
|
|
234
|
+
case _:
|
|
235
|
+
raise UnsupportedBuilderOperatingSystemError
|
|
236
|
+
toolkit = {(tk, which(tk) is not None) for tk in required_toolkit}
|
|
237
|
+
missing_toolkit = {tk for (tk, tk_is_installed) in toolkit if not tk_is_installed}
|
|
238
|
+
if missing_toolkit:
|
|
239
|
+
raise NoToolkitForAssemblingError(toolkit_required=missing_toolkit)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
|
|
3
|
+
from gofra.exceptions import GofraError
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class NoToolkitForAssemblingError(GofraError):
|
|
7
|
+
toolkit_required: Iterable[str]
|
|
8
|
+
|
|
9
|
+
def __init__(self, *args: object, toolkit_required: Iterable[str]) -> None:
|
|
10
|
+
super().__init__(*args)
|
|
11
|
+
self.toolkit_required = toolkit_required
|
|
12
|
+
|
|
13
|
+
def __repr__(self) -> str:
|
|
14
|
+
return f"Unable to assemble program due to not all toolkit installed, required: {','.join(self.toolkit_required)}"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class UnsupportedBuilderOperatingSystemError(GofraError):
|
|
18
|
+
def __repr__(self) -> str:
|
|
19
|
+
return "You are on unsupported operating system to compile that target"
|