Skip to content

Commit 729e174

Browse files
committed
added -h and --help flags
1 parent 291d3c6 commit 729e174

7 files changed

Lines changed: 55 additions & 27 deletions

File tree

CMakeLists.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(blang C CXX)
2+
project(blang VERSION 0.1.0 LANGUAGES C CXX)
33

44
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_C_STANDARD 11)
66
set(CMAKE_C_STANDARD_REQUIRED ON)
77

8+
89
set(CMAKE_BUILD_TYPE Debug)
910

1011
if (APPLE)
@@ -13,7 +14,6 @@ if (APPLE)
1314
list(APPEND CMAKE_PREFIX_PATH "/opt/homebrew/opt/llvm")
1415
endif()
1516

16-
1717
find_package(FLEX REQUIRED)
1818
find_package(BISON REQUIRED)
1919
find_package(LLVM REQUIRED CONFIG)
@@ -69,6 +69,13 @@ add_executable(blang
6969
${BISON_Parser_OUTPUTS}
7070
)
7171

72+
target_compile_definitions(blang PRIVATE
73+
BLANG_VERSION_MAJOR=${PROJECT_VERSION_MAJOR}
74+
BLANG_VERSION_MINOR=${PROJECT_VERSION_MINOR}
75+
BLANG_VERSION_PATCH=${PROJECT_VERSION_PATCH}
76+
BLANG_VERSION_STRING="${PROJECT_VERSION}"
77+
)
78+
7279
# Now target-specific commands
7380
target_include_directories(blang PRIVATE ${LLVM_INCLUDE_DIRS})
7481
target_include_directories(blang PRIVATE src ${CMAKE_CURRENT_BINARY_DIR})

src/binary.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646

4747
#include <llvm/TargetParser/Host.h>
4848

49-
// Optimize TheModule with the provided optimization flags (-O3, -Os, etc)
50-
void optimize();
51-
5249
extern "C" void initialize_llvm() {
5350
TheContext = std::make_unique<llvm::LLVMContext>();
5451
Builder = std::unique_ptr<llvm::IRBuilder<>>(new llvm::IRBuilder<>(*TheContext));
@@ -69,8 +66,6 @@ extern "C" void initialize_llvm() {
6966
}
7067

7168
extern "C" void export_asm() {
72-
optimize(); // Apply any optimizations (will return if -O0)
73-
7469
std::basic_string<char> targetTriple = llvm::sys::getDefaultTargetTriple();
7570
TheModule->setTargetTriple(targetTriple);
7671

@@ -117,8 +112,6 @@ extern "C" void export_asm() {
117112
}
118113

119114
extern "C" void export_ir() {
120-
optimize(); // Apply any optimizations (will return if -O0)
121-
122115
std::error_code EC;
123116
llvm::raw_fd_ostream dest("output.ll", EC, llvm::sys::fs::OF_None);
124117

@@ -129,8 +122,6 @@ extern "C" void export_ir() {
129122
}
130123

131124
extern "C" void generate_binary() {
132-
optimize(); // Apply any optimizations (will return if -O0)
133-
134125
std::basic_string<char> targetTriple = llvm::sys::getDefaultTargetTriple();
135126
TheModule->setTargetTriple(targetTriple);
136127

@@ -176,7 +167,7 @@ extern "C" void generate_binary() {
176167
dest.flush();
177168
}
178169

179-
inline void optimize() {
170+
extern "C" void optimize() {
180171
if (GCC_LIKELY(!ctx.optimization)) return;
181172

182173
// Create analysis managers

src/context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
typedef struct CompilerContext {
3131
bool emitAssembly;
3232
bool emitLLVM;
33+
bool dumpAST;
3334
char* outputFilename;
3435
char* inputFile;
3536
char* sourceText;

src/error.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
GCC_COLD void error(const char *text, ...) {
3232
va_list args;
3333
va_start(args, text);
34-
printf("bcc: " RED "error: " RESET);
34+
printf("blang: " RED "error: " RESET);
3535
vprintf(text, args);
3636
printf("\n");
3737
va_end(args);
@@ -40,9 +40,9 @@ GCC_COLD void error(const char *text, ...) {
4040
GCC_NORETURN GCC_COLD void fatal_error(const char *text, ...) {
4141
va_list args;
4242
va_start(args, text);
43-
printf("bcc: " RED "error: " RESET);
43+
printf("blang: " RED "error: " RESET);
4444
vprintf(text, args);
4545
va_end(args);
46-
printf("\nbcc: " RED "error: " RESET "compilation failed\n");
46+
printf("\nblang: " RED "error: " RESET "compilation failed\n");
4747
exit(EXIT_FAILURE);
4848
}

src/llvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ extern "C" {
3939

4040
void generate_llvm_ir();
4141
void initialize_llvm();
42+
4243
void generate_binary();
4344
void export_ir();
4445
void export_asm();
46+
void optimize();
4547

4648
#ifdef __cplusplus
4749
}

src/llvm_ir.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,16 @@ static void add_function(ASTNode* node) {
301301
// Create the function type, including arguments if any.
302302
llvm::FunctionType *funcType;
303303
if (list.size() == 0 )
304-
{
305304
funcType = llvm::FunctionType::get(
306305
llvm::Type::getInt64Ty(*TheContext),
307306
false
308307
);
309-
} else {
308+
else
310309
funcType = llvm::FunctionType::get(
311310
llvm::Type::getInt64Ty(*TheContext),
312311
list,
313312
false
314313
);
315-
}
316314

317315
llvm::Function *function = llvm::Function::Create(
318316
funcType,

src/main.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,30 @@ extern int yyparse(void); // declare Bison parser function
3434
extern int yy_scan_string(const char *str); // lex the file for keywords
3535
void parse_arguments(int argc, char **argv); // parse the arguments provided to BLang
3636
char* read_file(const char *filename); // Read an input file into a char*.
37+
void print_help();
3738

3839
CompilerContext ctx = (CompilerContext){
3940
.emitAssembly = false,
4041
.emitLLVM = false,
42+
.dumpAST = false,
4143
.outputFilename = "a.out",
4244
.optimization = 0,
4345
};
4446

4547
int main(int argc, char *argv[]) {
4648
parse_arguments(argc, argv);
47-
initialize_llvm();
4849

4950
yy_scan_string(ctx.sourceText); // Feed input
5051
yyparse(); // Start parsing
5152

52-
print_ast();
53+
if (ctx.dumpAST) print_ast();
54+
55+
initialize_llvm();
5356

5457
generate_llvm_ir();
5558

59+
optimize();
60+
5661
if (ctx.emitLLVM)
5762
export_ir();
5863
else if (ctx.emitAssembly)
@@ -65,15 +70,15 @@ int main(int argc, char *argv[]) {
6570

6671

6772
void parse_arguments(int argc, char *argv[]) {
68-
if (argc == 0)
69-
printf("--help function should print here. TODO.");
73+
if (argc == 1)
74+
print_help();
7075

7176
for (int i = 1; i < argc; ++i) {
72-
// If "-S" is passed, the program will return assembly instead of machine code.
7377
if (strcmp(argv[i], "-S") == 0) { ctx.emitAssembly = true; }
74-
75-
// If "-emit-llvm" is passed, the program will return LLVM IR instead of machine code.
76-
else if (strcmp(argv[i], "--emit-llvm") == 0) { ctx.emitLLVM = true; }
78+
else if (strcmp(argv[i], "-emit-llvm") == 0) { ctx.emitLLVM = true; }
79+
else if (strcmp(argv[i], "-ast-dump") == 0) { ctx.dumpAST = true; }
80+
81+
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { print_help(); }
7782

7883
else if (strcmp(argv[i], "-o") == 0) {
7984
ctx.outputFilename = argv[i + 1];
@@ -94,7 +99,7 @@ void parse_arguments(int argc, char *argv[]) {
9499
ctx.optimization = 5;
95100

96101
else {
97-
if (argv[i][0] == '-') { fatal_error("unknown argument: \'%s\'\n", argv[i]); }
102+
if (argv[i][0] == '-') { fatal_error("unknown argument: \'%s\'", argv[i]); }
98103
else {
99104
ctx.inputFile = argv[i];
100105
ctx.sourceText = read_file(argv[i]);
@@ -119,3 +124,27 @@ char* read_file(const char *filename) {
119124
fclose(f);
120125
return buf;
121126
}
127+
128+
void print_help() {
129+
printf(
130+
"Usage: blang [options] <source files>\n"
131+
"\n"
132+
"Blang Compiler " BLANG_VERSION_STRING "\n"
133+
"A simple compiler for the B programming language.\n"
134+
"\n"
135+
"Options:\n"
136+
" -h, --help Show this help message and exit\n"
137+
// " -v, --version Show compiler version\n"
138+
" -o <file> Specify output file name (default: a.out)\n"
139+
" -S Compile to assembly code only\n"
140+
" -emit-llvm Emit LLVM IR instead of machine code\n"
141+
" -dump-ast Output the abstract syntax tree (AST)\n"
142+
" -O0, -O1, -O2, -O3 Optimization level (default: -O0)\n"
143+
"\n"
144+
"Examples:\n"
145+
" blang main.b Compile and link main.b to a.out\n"
146+
" blang -S main.b Generate assembly code from main.b\n"
147+
" blang -O2 -o prog main.b Compile main.b with optimization level 2 to prog\n"
148+
);
149+
exit(0);
150+
}

0 commit comments

Comments
 (0)