Skip to content

Commit 9e405a7

Browse files
committed
fix: Restructured the code base
This change introduces libaries for parser, the IR and and the llvm backend.
1 parent 1747e63 commit 9e405a7

File tree

119 files changed

+342
-156
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+342
-156
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,36 @@ on:
99
jobs:
1010
build-linux:
1111
runs-on: ubuntu-latest
12+
container: ubuntu:22.04
1213

1314
steps:
1415
- uses: actions/checkout@v2
1516

17+
- name: Install required ubuntu packages
18+
env:
19+
DEBIAN_FRONTEND: noninteractive
20+
21+
run: |
22+
apt-get update && apt-get install -y --no-install-recommends \
23+
cmake \
24+
build-essential \
25+
llvm-dev \
26+
python3 \
27+
python3-pip \
28+
ninja-build \
29+
git
30+
1631
- name: Install unit tests requirements
1732
run: |
1833
pip install -r tests/unit_tests/requirements.txt
1934
2035
- name: Build
2136
run: |
22-
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release .
37+
cmake . \
38+
-G Ninja \
39+
-Bbuild \
40+
-DCMAKE_BUILD_TYPE=Release
41+
2342
cmake --build build
2443
2544
- name: Test
@@ -28,24 +47,19 @@ jobs:
2847
ctest --verbose
2948
3049
build-windows:
31-
runs-on: windows-latest
50+
runs-on: windows-2022
3251

3352
steps:
3453
- uses: actions/checkout@v2
3554

36-
- uses: actions/cache@v2
37-
id: vcpkg-cache
38-
with:
39-
path: ~\AppData\Local\vcpkg\archives
40-
key: vcpkg-cache
41-
4255
- name: Install unit tests requirements
4356
run: |
4457
pip install -r tests\unit_tests\requirements.txt
4558
4659
- name: Build
4760
run: |
48-
cmake -Bbuild -DCMAKE_TOOLCHAIN_FILE=C:\Vcpkg\scripts\buildsystems\vcpkg.cmake .
61+
cmake . -Bbuild
62+
4963
cmake --build build --config Release
5064
5165
- name: Test
@@ -86,7 +100,12 @@ jobs:
86100
- name: Build
87101
run: |
88102
. .env/bin/activate
89-
cmake -Bbuild -DCMAKE_BUILD_TYPE=Release .
103+
104+
cmake . \
105+
-Bbuild \
106+
-DCMAKE_BUILD_TYPE=Release \
107+
-DLLVM_ROOT=$(brew --prefix llvm)
108+
90109
cmake --build build
91110
92111
- name: Test

CMakeLists.txt

Lines changed: 8 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ file(READ "${CMAKE_CURRENT_SOURCE_DIR}/version.txt" version)
2929

3030
set(CMAKE_CXX_STANDARD 20)
3131

32+
find_package(LLVM 13 CONFIG QUIET)
33+
3234
FetchContent_Declare(
3335
fmt
3436
GIT_REPOSITORY https://github.com/fmtlib/fmt
@@ -58,70 +60,16 @@ FetchContent_MakeAvailable(nlohmann_json)
5860
add_library(nlohmann_json::nlohmann_json INTERFACE IMPORTED)
5961
set_target_properties(nlohmann_json::nlohmann_json PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${nlohmann_json_SOURCE_DIR}/single_include")
6062

61-
add_subdirectory(tools/kwgen)
62-
63-
aux_source_directory(src/cxx SOURCES)
64-
65-
add_library(cxx ${SOURCES}
66-
# generated files
67-
keywords-priv.h
68-
)
69-
70-
target_include_directories(cxx
71-
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}
72-
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
73-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
74-
)
75-
76-
target_link_libraries(cxx
77-
PUBLIC $<BUILD_INTERFACE:fmt::fmt-header-only>
78-
$<BUILD_INTERFACE:utf8::cpp>
79-
)
80-
81-
add_custom_command(OUTPUT keywords-priv.h
82-
COMMAND kwgen < ${CMAKE_CURRENT_SOURCE_DIR}/src/cxx/keywords.kwgen > keywords-priv.h
83-
DEPENDS kwgen src/cxx/keywords.kwgen
84-
COMMENT "Generate keywords-priv.h")
85-
86-
aux_source_directory(src/frontend FRONTEND_SOURCES)
87-
88-
add_executable(cxx-frontend ${FRONTEND_SOURCES})
89-
target_link_libraries(cxx-frontend cxx nlohmann_json::nlohmann_json)
90-
91-
if(EMSCRIPTEN)
92-
target_link_options(cxx-frontend PUBLIC
93-
"SHELL:-s EXIT_RUNTIME=1"
94-
"SHELL:-s NODERAWFS=1"
95-
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
96-
)
97-
endif()
98-
99-
if(EMSCRIPTEN)
100-
101-
add_executable(cxx-js src/cxx-js/api.cc)
102-
103-
target_link_libraries(cxx-js cxx)
104-
105-
target_link_options(cxx-js PUBLIC
106-
"SHELL:--bind"
107-
"SHELL:-s ENVIRONMENT=web"
108-
"SHELL:-s WASM_ASYNC_COMPILATION=1"
109-
"SHELL:-s MODULARIZE=1"
110-
"SHELL:-s MALLOC=emmalloc"
111-
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
112-
"SHELL:-s FILESYSTEM=1"
113-
"SHELL:-s NO_DYNAMIC_EXECUTION=1"
114-
"SHELL:--extern-post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/cxx-js/suffix.js"
115-
)
116-
117-
endif()
118-
11963
enable_testing()
12064

65+
add_subdirectory(tools/kwgen)
66+
add_subdirectory(src/parser)
67+
add_subdirectory(src/ir)
68+
add_subdirectory(src/llvm)
69+
add_subdirectory(src/js)
70+
add_subdirectory(src/frontend)
12171
add_subdirectory(tests)
12272

123-
file(GLOB CXX_INCLUDE_HEADER_FILES src/cxx/*.h)
124-
12573
configure_package_config_file(
12674
${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
12775
"${CMAKE_CURRENT_BINARY_DIR}/cxxConfig.cmake"
@@ -134,27 +82,13 @@ write_basic_package_version_file(
13482
COMPATIBILITY AnyNewerVersion
13583
)
13684

137-
install(
138-
TARGETS cxx
139-
EXPORT cxxTargets
140-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
141-
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
142-
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
143-
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
144-
)
145-
14685
install(
14786
EXPORT cxxTargets
14887
FILE cxxTargets.cmake
14988
NAMESPACE cxx::
15089
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/cxx
15190
)
15291

153-
install(
154-
FILES ${CXX_INCLUDE_HEADER_FILES}
155-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cxx
156-
)
157-
15892
install(FILES
15993
"${CMAKE_CURRENT_BINARY_DIR}/cxxConfig.cmake"
16094
"${CMAKE_CURRENT_BINARY_DIR}/cxxConfigVersion.cmake"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ On Linux, macOS and Windows:
2424
# install the python packages required to run the unit tests
2525
pip install -r tests/unit_tests/requirements.txt
2626

27-
# configure cxx-frontend
27+
# configure the source code
2828
cmake . \
2929
-G Ninja \
3030
-B build \
3131
-DCMAKE_BUILD_TYPE=Release \
3232
-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=1
3333

34-
# build cxx-frontend
34+
# build
3535
cmake --build build
3636

3737
# run the unit tests
@@ -45,7 +45,7 @@ Use `-ast-dump` to dump the AST of a C++ program.
4545

4646
```sh
4747
echo 'int main() { auto f = []{ return 1; }; return f(); }' |
48-
./build/cxx-frontend -ast-dump -
48+
./build/src/frontend/cxx -ast-dump -
4949
```
5050

5151
## Build the npm package using docker

packages/cxx-frontend/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"docker:shell": "DOCKER_EXTRA_OPTS=-it npm run docker",
1515
"configure:wasm": "npm run docker -- emcmake cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INTERPROCEDURAL_OPTIMIZATION=1 -Bbuild.em .",
1616
"build:wasm": "npm run docker -- cmake --build build.em -- -j4",
17-
"copy:wasm": "copyfiles -V -f ../../build.em/cxx-js.* dist",
17+
"copy:wasm": "copyfiles -V -f ../../build.em/src/js/cxx-js.* dist",
1818
"copy:readme": "copyfiles -V -f ../../README.md ../../CHANGELOG.md ."
1919
},
2020
"keywords": [
@@ -40,4 +40,4 @@
4040
"copyfiles": "^2.4.1",
4141
"typescript": "^4.5.5"
4242
}
43-
}
43+
}

src/frontend/CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright (c) 2022 Roberto Raggi <[email protected]>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
# this software and associated documentation files (the "Software"), to deal in
5+
# the Software without restriction, including without limitation the rights to
6+
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
# the Software, and to permit persons to whom the Software is furnished to do so,
8+
# subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in all
11+
# copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19+
20+
aux_source_directory(cxx SOURCES)
21+
22+
add_executable(cxx ${SOURCES})
23+
24+
target_link_libraries(cxx
25+
cxx-parser
26+
cxx-ir
27+
nlohmann_json::nlohmann_json
28+
)
29+
30+
if (LLVM_FOUND)
31+
target_link_libraries(cxx cxx-llvm)
32+
endif()
33+
34+
if(EMSCRIPTEN)
35+
target_link_options(cxx PUBLIC
36+
"SHELL:-s EXIT_RUNTIME=1"
37+
"SHELL:-s NODERAWFS=1"
38+
"SHELL:-s ALLOW_MEMORY_GROWTH=1"
39+
)
40+
endif()
41+
42+
install(
43+
TARGETS cxx
44+
EXPORT cxxTargets
45+
)
File renamed without changes.
File renamed without changes.

src/frontend/cli.cc renamed to src/frontend/cxx/cli.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ void CLI::parse(int& argc, char**& argv) {
274274
}
275275

276276
void CLI::showHelp() {
277-
fmt::print(stderr, "Usage: cxx-frontend [options] file...\n");
277+
fmt::print(stderr, "Usage: cxx [options] file...\n");
278278
fmt::print(stderr, "Options:\n");
279279
for (const auto& opt : options) {
280280
if (opt.visibility == CLIOptionVisibility::kExperimental) {
File renamed without changes.

src/frontend/frontend.cc renamed to src/frontend/cxx/frontend.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
// cxx
2222
#include <cxx/ast.h>
2323
#include <cxx/ast_visitor.h>
24-
#include <cxx/codegen.h>
2524
#include <cxx/control.h>
2625
#include <cxx/gcc_linux_toolchain.h>
27-
#include <cxx/ir.h>
28-
#include <cxx/ir_printer.h>
26+
#include <cxx/ir/codegen.h>
27+
#include <cxx/ir/ir.h>
28+
#include <cxx/ir/ir_printer.h>
29+
#include <cxx/ir/x64_instruction_selection.h>
2930
#include <cxx/lexer.h>
3031
#include <cxx/macos_toolchain.h>
3132
#include <cxx/preprocessor.h>
@@ -35,7 +36,6 @@
3536
#include <cxx/symbols.h>
3637
#include <cxx/translation_unit.h>
3738
#include <cxx/windows_toolchain.h>
38-
#include <cxx/x64_instruction_selection.h>
3939

4040
#include "ast_printer.h"
4141

@@ -336,12 +336,12 @@ bool runOnFile(const CLI& cli, const std::string& fileName) {
336336
}
337337

338338
if (cli.opt_S || cli.opt_ir_dump || cli.opt_c) {
339-
Codegen cg;
339+
ir::Codegen cg;
340340

341341
auto module = cg(&unit);
342342

343343
if (cli.opt_S || cli.opt_c) {
344-
X64InstructionSelection isel;
344+
ir::X64InstructionSelection isel;
345345
isel(module.get(), output);
346346
} else if (cli.opt_ir_dump) {
347347
ir::IRPrinter printer;
@@ -375,8 +375,8 @@ int main(int argc, char* argv[]) {
375375
const auto& inputFiles = cli.positionals();
376376

377377
if (inputFiles.empty()) {
378-
std::cerr << "cxx-frontend: no input files" << std::endl
379-
<< "Usage: cxx-frontend [options] file..." << std::endl;
378+
std::cerr << "cxx: no input files" << std::endl
379+
<< "Usage: cxx [options] file..." << std::endl;
380380
return EXIT_FAILURE;
381381
}
382382

0 commit comments

Comments
 (0)