Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit b5d0ca1

Browse files
authored
refactor: move all double data type to float (#98)
Move all double data type to float. This is not really necessary, because soon it should allow both float and double, but we are doing it now in order to identify problems with incompatibility and we need to make the code more flexible.
1 parent bd63052 commit b5d0ca1

File tree

13 files changed

+94
-75
lines changed

13 files changed

+94
-75
lines changed

src/codegen/arx-llvm.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ std::map<std::string, llvm::AllocaInst*> ArxLLVM::named_values;
1818
std::map<std::string, std::unique_ptr<PrototypeAST>> ArxLLVM::function_protos;
1919

2020
/* Data types */
21-
llvm::Type* ArxLLVM::DOUBLE_TYPE;
2221
llvm::Type* ArxLLVM::FLOAT_TYPE;
22+
llvm::Type* ArxLLVM::DOUBLE_TYPE;
2323
llvm::Type* ArxLLVM::INT8_TYPE;
2424
llvm::Type* ArxLLVM::INT32_TYPE;
2525

2626
/* Debug Information Data types */
27-
llvm::DIType* ArxLLVM::DI_DOUBLE_TYPE;
2827
llvm::DIType* ArxLLVM::DI_FLOAT_TYPE;
28+
llvm::DIType* ArxLLVM::DI_DOUBLE_TYPE;
2929
llvm::DIType* ArxLLVM::DI_INT8_TYPE;
3030
llvm::DIType* ArxLLVM::DI_INT32_TYPE;
3131
llvm::DIType* ArxLLVM::DI_VOID_TYPE;
@@ -52,3 +52,22 @@ auto ArxLLVM::get_data_type(std::string type_name) -> llvm::Type* {
5252
llvm::errs() << "[EE] type_name not valid.\n";
5353
return nullptr;
5454
}
55+
56+
auto ArxLLVM::get_di_data_type(std::string di_type_name) -> llvm::DIType* {
57+
if (di_type_name == "float") {
58+
return ArxLLVM::DI_FLOAT_TYPE;
59+
} else if (di_type_name == "double") {
60+
return ArxLLVM::DI_DOUBLE_TYPE;
61+
} else if (di_type_name == "int8") {
62+
return ArxLLVM::DI_INT8_TYPE;
63+
} else if (di_type_name == "int32") {
64+
return ArxLLVM::DI_INT32_TYPE;
65+
} else if (di_type_name == "char") {
66+
return ArxLLVM::DI_INT8_TYPE;
67+
} else if (di_type_name == "void") {
68+
return ArxLLVM::DI_VOID_TYPE;
69+
}
70+
71+
llvm::errs() << "[EE] di_type_name not valid.\n";
72+
return nullptr;
73+
}

src/codegen/arx-llvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ArxLLVM {
3434
static llvm::DIType* DI_VOID_TYPE;
3535

3636
static auto get_data_type(std::string type_name) -> llvm::Type*;
37+
static auto get_di_data_type(std::string type_name) -> llvm::DIType*;
3738
};
3839

3940
extern bool IS_BUILD_LIB;

src/codegen/ast-to-llvm-ir.cpp

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ extern std::string ARX_VERSION;
4545
auto ASTToLLVMIRVisitor::CreateFunctionType(unsigned NumArgs)
4646
-> llvm::DISubroutineType* {
4747
llvm::SmallVector<llvm::Metadata*, 8> EltTys;
48-
llvm::DIType* DblTy = this->getDoubleTy();
4948

5049
// Add the result type.
51-
EltTys.emplace_back(DblTy);
50+
EltTys.emplace_back(ArxLLVM::DI_FLOAT_TYPE);
5251

5352
for (unsigned i = 0, e = NumArgs; i != e; ++i) {
54-
EltTys.emplace_back(DblTy);
53+
EltTys.emplace_back(ArxLLVM::DI_FLOAT_TYPE);
5554
}
5655

5756
return ArxLLVM::di_builder->createSubroutineType(
@@ -60,30 +59,20 @@ auto ASTToLLVMIRVisitor::CreateFunctionType(unsigned NumArgs)
6059

6160
// DebugInfo
6261

63-
auto ASTToLLVMIRVisitor::getDoubleTy() -> llvm::DIType* {
64-
if (this->DblTy) {
65-
return this->DblTy;
66-
}
67-
68-
DblTy = ArxLLVM::di_builder->createBasicType(
69-
"double", 64, llvm::dwarf::DW_ATE_float);
70-
return DblTy;
71-
}
72-
7362
auto ASTToLLVMIRVisitor::emitLocation(ExprAST& ast) -> void {
7463
if (!std::addressof(ast)) {
7564
return ArxLLVM::ir_builder->SetCurrentDebugLocation(llvm::DebugLoc());
7665
}
7766

78-
llvm::DIScope* Scope;
67+
llvm::DIScope* di_scope;
7968
if (this->llvm_di_lexical_blocks.empty()) {
80-
Scope = llvm_di_compile_unit;
69+
di_scope = llvm_di_compile_unit;
8170
} else {
82-
Scope = this->llvm_di_lexical_blocks.back();
71+
di_scope = this->llvm_di_lexical_blocks.back();
8372
}
8473

8574
ArxLLVM::ir_builder->SetCurrentDebugLocation(llvm::DILocation::get(
86-
Scope->getContext(), ast.get_line(), ast.getCol(), Scope));
75+
di_scope->getContext(), ast.get_line(), ast.getCol(), di_scope));
8776
}
8877

8978
/**
@@ -191,26 +180,26 @@ auto ASTToLLVMIRVisitor::visit(FunctionAST& expr) -> void {
191180

192181
/* debugging-code:start*/
193182
// Create a subprogram DIE for this function.
194-
llvm::DIFile* Unit = ArxLLVM::di_builder->createFile(
183+
llvm::DIFile* di_unit = ArxLLVM::di_builder->createFile(
195184
this->llvm_di_compile_unit->getFilename(),
196185
this->llvm_di_compile_unit->getDirectory());
197-
llvm::DIScope* FContext = Unit;
198-
unsigned LineNo = proto.get_line();
199-
unsigned ScopeLine = LineNo;
200-
llvm::DISubprogram* SP = ArxLLVM::di_builder->createFunction(
201-
FContext,
186+
llvm::DIScope* file_context = di_unit;
187+
unsigned line_no = proto.get_line();
188+
unsigned ScopeLine = line_no;
189+
llvm::DISubprogram* di_subprogram = ArxLLVM::di_builder->createFunction(
190+
file_context,
202191
proto.get_name(),
203192
llvm::StringRef(),
204-
Unit,
205-
LineNo,
193+
di_unit,
194+
line_no,
206195
CreateFunctionType(the_function->arg_size()),
207196
ScopeLine,
208197
llvm::DINode::FlagPrototyped,
209198
llvm::DISubprogram::SPFlagDefinition);
210-
the_function->setSubprogram(SP);
199+
the_function->setSubprogram(di_subprogram);
211200

212201
// Push the current scope.
213-
this->llvm_di_lexical_blocks.emplace_back(SP);
202+
this->llvm_di_lexical_blocks.emplace_back(di_subprogram);
214203

215204
// Unset the location for the prologue emission (leading instructions with no
216205
// location in a function are considered part of the prologue and the
@@ -224,28 +213,30 @@ auto ASTToLLVMIRVisitor::visit(FunctionAST& expr) -> void {
224213
// std::cout << "Record the function arguments in the named_values map.";
225214
ArxLLVM::named_values.clear();
226215

227-
unsigned ArgIdx = 0;
216+
unsigned arg_idx = 0;
228217
for (auto& llvm_arg : the_function->args()) {
229218
// Create an alloca for this variable.
230219
llvm::AllocaInst* alloca =
231220
CreateEntryBlockAlloca(the_function, llvm_arg.getName());
232221

233222
/* debugging-code: start */
234223
// Create a debug descriptor for the variable.
235-
llvm::DILocalVariable* D = ArxLLVM::di_builder->createParameterVariable(
236-
SP,
237-
llvm_arg.getName(),
238-
++ArgIdx,
239-
Unit,
240-
LineNo,
241-
this->getDoubleTy(),
242-
true);
224+
llvm::DILocalVariable* di_local_variable =
225+
ArxLLVM::di_builder->createParameterVariable(
226+
di_subprogram,
227+
llvm_arg.getName(),
228+
++arg_idx,
229+
di_unit,
230+
line_no,
231+
ArxLLVM::DI_FLOAT_TYPE,
232+
true);
243233

244234
ArxLLVM::di_builder->insertDeclare(
245235
alloca,
246-
D,
236+
di_local_variable,
247237
ArxLLVM::di_builder->createExpression(),
248-
llvm::DILocation::get(SP->getContext(), LineNo, 0, SP),
238+
llvm::DILocation::get(
239+
di_subprogram->getContext(), line_no, 0, di_subprogram),
249240
ArxLLVM::ir_builder->GetInsertBlock());
250241

251242
/* debugging-code-end */
@@ -297,6 +288,16 @@ auto ASTToLLVMIRVisitor::initialize() -> void {
297288
ArxLLVM::module->setDataLayout(ArxLLVM::jit->get_data_layout());
298289
/** Create a new builder for the module. */
299290
ArxLLVM::di_builder = std::make_unique<llvm::DIBuilder>(*ArxLLVM::module);
291+
292+
/* di data types */
293+
ArxLLVM::DI_FLOAT_TYPE = ArxLLVM::di_builder->createBasicType(
294+
"float", 32, llvm::dwarf::DW_ATE_float);
295+
ArxLLVM::DI_DOUBLE_TYPE = ArxLLVM::di_builder->createBasicType(
296+
"double", 64, llvm::dwarf::DW_ATE_float);
297+
ArxLLVM::DI_INT8_TYPE = ArxLLVM::di_builder->createBasicType(
298+
"int8", 8, llvm::dwarf::DW_ATE_signed);
299+
ArxLLVM::DI_INT32_TYPE = ArxLLVM::di_builder->createBasicType(
300+
"int32", 32, llvm::dwarf::DW_ATE_signed);
300301
}
301302

302303
/**

src/codegen/ast-to-llvm-ir.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <vector> // for vector
4+
35
#include <llvm/ADT/APFloat.h>
46
#include <llvm/ADT/Optional.h>
57
#include <llvm/ADT/STLExtras.h>
@@ -20,6 +22,7 @@
2022
#include <llvm/IR/Type.h>
2123
#include <llvm/IR/Verifier.h>
2224
#include <llvm/MC/TargetRegistry.h>
25+
#include <llvm/Support/Error.h> // for ExitOnError
2326
#include <llvm/Support/FileSystem.h>
2427
#include <llvm/Support/Host.h>
2528
#include <llvm/Support/raw_ostream.h>
@@ -38,7 +41,6 @@ class ASTToLLVMIRVisitor : public ASTToObjectVisitor {
3841
public:
3942
// DebugInfo
4043
llvm::DICompileUnit* llvm_di_compile_unit;
41-
llvm::DIType* DblTy;
4244
std::vector<llvm::DIScope*> llvm_di_lexical_blocks;
4345

4446
llvm::ExitOnError exit_on_err;
@@ -61,5 +63,4 @@ class ASTToLLVMIRVisitor : public ASTToObjectVisitor {
6163

6264
// DebugInfo
6365
void emitLocation(ExprAST& AST);
64-
llvm::DIType* getDoubleTy();
6566
};

src/codegen/ast-to-object.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#include <cassert> // for assert
22
#include <cstdio> // for fprintf, stderr, fputc
33
#include <cstdlib> // for exit
4-
#include <fstream> // for operator<<
54
#include <iostream>
6-
#include <map> // for map, operator==, _Rb_tree_ite...
7-
#include <memory> // for unique_ptr, allocator, make_u...
8-
#include <numeric> // for accumulate
9-
#include <sstream>
5+
#include <map> // for map, operator==, _Rb_tree_ite...
6+
#include <memory> // for unique_ptr, allocator, make_u...
107
#include <string> // for string, operator<=>, operator+
118
#include <system_error> // for error_code
129
#include <utility> // for pair, move
@@ -101,7 +98,7 @@ auto ASTToObjectVisitor::getFunction(std::string name) -> void {
10198
auto ASTToObjectVisitor::CreateEntryBlockAlloca(
10299
llvm::Function* fn, llvm::StringRef var_name) -> llvm::AllocaInst* {
103100
llvm::IRBuilder<> TmpB(&fn->getEntryBlock(), fn->getEntryBlock().begin());
104-
return TmpB.CreateAlloca(ArxLLVM::DOUBLE_TYPE, nullptr, var_name);
101+
return TmpB.CreateAlloca(ArxLLVM::FLOAT_TYPE, nullptr, var_name);
105102
}
106103

107104
/**
@@ -136,7 +133,7 @@ auto ASTToObjectVisitor::visit(VariableExprAST& expr) -> void {
136133
}
137134

138135
this->result_val = ArxLLVM::ir_builder->CreateLoad(
139-
ArxLLVM::DOUBLE_TYPE, expr_var, expr.name.c_str());
136+
ArxLLVM::FLOAT_TYPE, expr_var, expr.name.c_str());
140137
}
141138

142139
/**
@@ -227,9 +224,9 @@ auto ASTToObjectVisitor::visit(BinaryExprAST& expr) -> void {
227224
case '<':
228225
llvm_val_lhs = ArxLLVM::ir_builder->CreateFCmpULT(
229226
llvm_val_lhs, llvm_val_rhs, "cmptmp");
230-
// Convert bool 0/1 to double 0.0 or 1.0 //
227+
// Convert bool 0/1 to float 0.0 or 1.0 //
231228
this->result_val = ArxLLVM::ir_builder->CreateUIToFP(
232-
llvm_val_lhs, ArxLLVM::DOUBLE_TYPE, "booltmp");
229+
llvm_val_lhs, ArxLLVM::FLOAT_TYPE, "booltmp");
233230
return;
234231
}
235232

@@ -341,7 +338,7 @@ auto ASTToObjectVisitor::visit(IfExprAST& expr) -> void {
341338
fn->getBasicBlockList().push_back(MergeBB);
342339
ArxLLVM::ir_builder->SetInsertPoint(MergeBB);
343340
llvm::PHINode* PN =
344-
ArxLLVM::ir_builder->CreatePHI(ArxLLVM::DOUBLE_TYPE, 2, "iftmp");
341+
ArxLLVM::ir_builder->CreatePHI(ArxLLVM::FLOAT_TYPE, 2, "iftmp");
345342

346343
PN->addIncoming(ThenV, ThenBB);
347344
PN->addIncoming(ElseV, ElseBB);
@@ -426,7 +423,7 @@ auto ASTToObjectVisitor::visit(ForExprAST& expr) -> void {
426423
// Reload, increment, and restore the alloca. This handles the case
427424
// where the body of the loop mutates the variable.
428425
llvm::Value* CurVar = ArxLLVM::ir_builder->CreateLoad(
429-
ArxLLVM::DOUBLE_TYPE, alloca, expr.var_name.c_str());
426+
ArxLLVM::FLOAT_TYPE, alloca, expr.var_name.c_str());
430427
llvm::Value* NextVar =
431428
ArxLLVM::ir_builder->CreateFAdd(CurVar, StepVal, "nextvar");
432429
ArxLLVM::ir_builder->CreateStore(NextVar, alloca);
@@ -455,7 +452,7 @@ auto ASTToObjectVisitor::visit(ForExprAST& expr) -> void {
455452
}
456453

457454
// for expr always returns 0.0.
458-
this->result_val = llvm::Constant::getNullValue(ArxLLVM::DOUBLE_TYPE);
455+
this->result_val = llvm::Constant::getNullValue(ArxLLVM::FLOAT_TYPE);
459456
}
460457

461458
/**
@@ -523,8 +520,8 @@ auto ASTToObjectVisitor::visit(VarExprAST& expr) -> void {
523520
*
524521
*/
525522
auto ASTToObjectVisitor::visit(PrototypeAST& expr) -> void {
526-
std::vector<llvm::Type*> args_type(expr.args.size(), ArxLLVM::DOUBLE_TYPE);
527-
llvm::Type* return_type = ArxLLVM::get_data_type("double");
523+
std::vector<llvm::Type*> args_type(expr.args.size(), ArxLLVM::FLOAT_TYPE);
524+
llvm::Type* return_type = ArxLLVM::get_data_type("float");
528525

529526
llvm::FunctionType* fn_type =
530527
llvm::FunctionType::get(return_type, args_type, false /* isVarArg */);
@@ -616,7 +613,7 @@ auto ASTToObjectVisitor::initialize() -> void {
616613
ArxLLVM::ir_builder = std::make_unique<llvm::IRBuilder<>>(*ArxLLVM::context);
617614

618615
/* Data Types */
619-
ArxLLVM::DOUBLE_TYPE = llvm::Type::getFloatTy(*ArxLLVM::context);
616+
ArxLLVM::FLOAT_TYPE = llvm::Type::getFloatTy(*ArxLLVM::context);
620617
ArxLLVM::DOUBLE_TYPE = llvm::Type::getDoubleTy(*ArxLLVM::context);
621618
ArxLLVM::INT8_TYPE = llvm::Type::getInt8Ty(*ArxLLVM::context);
622619
ArxLLVM::INT32_TYPE = llvm::Type::getInt32Ty(*ArxLLVM::context);
@@ -644,19 +641,19 @@ auto ASTToObjectVisitor::main_loop(TreeAST& ast) -> void {
644641
#endif
645642

646643
/**
647-
* @brief putchar that takes a double and returns 0.
644+
* @brief putchar that takes a float and returns 0.
648645
*
649646
*/
650-
extern "C" DLLEXPORT auto putchard(double X) -> double {
647+
extern "C" DLLEXPORT auto putchard(float X) -> float {
651648
fputc(static_cast<char>(X), stderr);
652649
return 0;
653650
}
654651

655652
/**
656-
* @brief printf that takes a double prints it as "%f\n", returning 0.
653+
* @brief printf that takes a float prints it as "%f\n", returning 0.
657654
*
658655
*/
659-
extern "C" DLLEXPORT auto printd(double X) -> double {
656+
extern "C" DLLEXPORT auto printd(float X) -> float {
660657
fprintf(stderr, "%f\n", X);
661658
return 0;
662659
}

src/lexer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ SourceLocation Lexer::cur_loc;
99
// Filled in if tok_identifier
1010
std::string Lexer::identifier_str = "<NOT DEFINED>";
1111
// Filled in if tok_float_literal
12-
double Lexer::num_float;
12+
float Lexer::num_float;
1313
SourceLocation Lexer::lex_loc;
1414
int Lexer::cur_tok = tok_not_initialized;
1515

src/lexer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Lexer {
4747
public:
4848
static SourceLocation cur_loc;
4949
static std::string identifier_str; // Filled in if tok_identifier
50-
static double num_float; // Filled in if tok_float_literal
50+
static float num_float; // Filled in if tok_float_literal
5151
static int cur_tok;
5252
static SourceLocation lex_loc;
5353

src/parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ class ExprAST {
9898
*/
9999
class FloatExprAST : public ExprAST {
100100
public:
101-
double val;
101+
float val;
102102

103-
FloatExprAST(double _val) : val(_val) {
103+
FloatExprAST(float _val) : val(_val) {
104104
this->kind = ExprKind::FloatDTKind;
105105
}
106106

tests/integration/average.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include <iostream>
22

33
extern "C" {
4-
double average(double, double);
4+
float average(float, float);
55
}
66

77
int main() {
8-
double avg_3_4 = average(3.0, 4.0);
8+
float avg_3_4 = average(3.0, 4.0);
99
std::cout << "average of 3.0 and 4.0: " << avg_3_4 << std::endl;
1010
}

tests/integration/constant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#include <iostream>
22

33
extern "C" {
4-
double get_constant(double);
4+
float get_constant(float);
55
}
66

77
int main() {
8-
double value = get_constant(3.0);
8+
float value = get_constant(3.0);
99
printf("get_constant(3.0): %f\n", value);
1010

1111
return 0;

0 commit comments

Comments
 (0)