Skip to content

Commit dd39c5b

Browse files
authored
Merge pull request #65 from brain-labs/cell-bit-size
Adding custom cell width size
2 parents ebbc5d4 + 9e0b347 commit dd39c5b

20 files changed

+243
-52
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ Example of the instructions above:
155155

156156
- `--code=<"inline code">` Sets inline brain code
157157
- `--io=repl` Sets the IO module to REPLs style
158-
- `--out=<filename>` Sets the output filename
159-
- `--size=<number>` Sets the number of cells used by the interpreter
158+
- `--out=<filename>` Sets the output filename
159+
- `--size=<number>` Sets the number of cells used by the interpreter
160+
- `--size-cell=<number>` Sets the cell width (`8`, `16`, `32`, `64` bits) of each cell
160161
- `--version` Shows the current version of Brain
161162
- `-emit-llvm` Emits LLVM IR code for the given input
162163
- `-emit-ast` Emits the AST for the given input

libs/io_16.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// -------------- 16 bits
2+
3+
#include <stdio.h>
4+
5+
// you can overwrite those functions! :)
6+
7+
void b_getchar_16(short idx, short *cells, int size) {
8+
cells[idx] = (short)getchar();
9+
}
10+
11+
void b_putchar_16(short idx, short *cells, int size) {
12+
putchar((int)cells[idx]);
13+
}
14+
15+
void b_float_print_16(short idx, short *cells, int size) {
16+
float value = cells[idx] / 100.0;
17+
printf("%.2f", value);
18+
}
19+
20+
void b_debug_16(short idx, short *cells, int size) {
21+
printf(
22+
"Index Pointer: %d Value at Index Pointer: %d\n",
23+
idx,
24+
cells[idx]
25+
);
26+
}

libs/io.c renamed to libs/io_32.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// -------------- 32 bits
2+
13
#include <stdio.h>
24
#include <stdlib.h>
35
#include <stdbool.h>
@@ -75,20 +77,19 @@ void b_show_tape(int idx, int *cells, int size) {
7577
printf("*(%d)\n", idx);
7678
}
7779

78-
void b_getchar(int idx, int *cells, int size) {
80+
void b_getchar_32(int idx, int *cells, int size) {
7981
cells[idx] = getchar();
8082
}
8183

82-
void b_putchar(int idx, int *cells, int size) {
84+
void b_putchar_32(int idx, int *cells, int size) {
8385
putchar(cells[idx]);
8486
}
8587

86-
void b_float_print(int idx, int *cells, int size) {
88+
void b_float_print_32(int idx, int *cells, int size) {
8789
float value = cells[idx] / 100.0;
8890
printf("%.2f", value);
8991
}
9092

91-
void b_debug(int idx, int *cells, int size) {
93+
void b_debug_32(int idx, int *cells, int size) {
9294
b_show_tape(idx, cells, size);
9395
}
94-

libs/io_64.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// -------------- 64 bits
2+
3+
#include <stdio.h>
4+
5+
// you can overwrite those functions! :)
6+
7+
void b_getchar_64(long idx, long *cells, int size) {
8+
cells[idx] = (long)getchar();
9+
}
10+
11+
void b_putchar_64(long idx, long *cells, int size) {
12+
putchar((int)cells[idx]);
13+
}
14+
15+
void b_float_print_64(long idx, long *cells, int size) {
16+
float value = cells[idx] / 100.0;
17+
printf("%.2f", value);
18+
}
19+
20+
void b_debug_64(long idx, long *cells, int size) {
21+
printf(
22+
"Index Pointer: %ld Value at Index Pointer: %ld\n",
23+
idx,
24+
cells[idx]
25+
);
26+
}

libs/io_8.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// -------------- 8 bits
2+
3+
#include <stdio.h>
4+
5+
// you can overwrite those functions! :)
6+
7+
void b_getchar_8(char idx, char *cells, int size) {
8+
cells[idx] = (char)getchar();
9+
}
10+
11+
void b_putchar_8(char idx, char *cells, int size) {
12+
putchar((int)cells[idx]);
13+
}
14+
15+
void b_float_print_8(char idx, char *cells, int size) {
16+
float value = cells[idx] / 100.0;
17+
printf("%.2f", value);
18+
}
19+
20+
void b_debug_8(char idx, char *cells, int size) {
21+
printf(
22+
"Index Pointer: %d Value at Index Pointer: %d\n",
23+
idx,
24+
cells[idx]
25+
);
26+
}

src/ast/general/ASTInfo.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,27 @@ llvm::GlobalVariable* ASTInfo::get_cells_size()
4141
return __brain_cells_size;
4242
}
4343

44+
llvm::Type* ASTInfo::get_cell_type(llvm::LLVMContext &C)
45+
{
46+
return llvm::Type::getIntNTy(C,
47+
ArgsOptions::instance()->get_cell_bitsize());
48+
}
49+
50+
llvm::Type* ASTInfo::get_cell_ptr_type(llvm::LLVMContext &C)
51+
{
52+
return llvm::Type::getIntNPtrTy(C,
53+
ArgsOptions::instance()->get_cell_bitsize());
54+
}
55+
4456
void ASTInfo::code_gen(llvm::Module *M, llvm::IRBuilder<> &B)
4557
{
4658
llvm::LLVMContext &context = M->getContext();
4759

4860
if (!__brain_index_ptr) {
4961
// Create global variable |brain.index|
50-
llvm::Type *Ty = llvm::Type::getInt32Ty(context);
51-
const llvm::APInt Zero = llvm::APInt(32, 0); // int32 0
62+
llvm::Type *Ty = ASTInfo::_instance->get_cell_type(context);
63+
const llvm::APInt Zero = llvm::APInt(ArgsOptions::instance()->get_cell_bitsize(),
64+
0); // int 8, 16, 32 or with value of 0
5265
llvm::Constant *InitV = llvm::Constant::getIntegerValue(Ty, Zero);
5366
ASTInfo::__brain_index_ptr = new llvm::GlobalVariable(*M, Ty, false,
5467
// Keep one copy when linking (weak)
@@ -60,7 +73,7 @@ void ASTInfo::code_gen(llvm::Module *M, llvm::IRBuilder<> &B)
6073
if (!__brain_cells_size) {
6174
// Create global variable |brain.cells.size|
6275
llvm::Type *Ty = llvm::Type::getInt32Ty(context);
63-
// Int32 tape size
76+
// int 8, 16, 32 or 64 tape size
6477
const llvm::APInt Size = llvm::APInt(32, ArgsOptions::instance()->get_cells_size());
6578
llvm::Constant *InitV = llvm::Constant::getIntegerValue(Ty, Size);
6679
ASTInfo::__brain_cells_size = new llvm::GlobalVariable(*M, Ty, false,
@@ -72,11 +85,13 @@ void ASTInfo::code_gen(llvm::Module *M, llvm::IRBuilder<> &B)
7285

7386
if (!__brain_cells_ptr) {
7487
// Create |brain.cells|
75-
auto *ArrTy = llvm::ArrayType::get(llvm::Type::getInt32Ty(context),
88+
auto *ArrTy = llvm::ArrayType::get(ASTInfo::_instance->get_cell_type(context),
7689
ArgsOptions::instance()->get_cells_size());
7790
// Create a vector of _k_cells_size items equal to 0
78-
std::vector<llvm::Constant *> constants(ArgsOptions::instance()->get_cells_size(),
79-
B.getInt32(0));
91+
std::vector<llvm::Constant *> constants(
92+
ArgsOptions::instance()->get_cells_size(),
93+
B.getIntN(ArgsOptions::instance()->get_cell_bitsize(), 0)
94+
);
8095
llvm::ArrayRef<llvm::Constant *> Constants = llvm::ArrayRef<llvm::Constant *>(constants);
8196
llvm::Constant *InitPtr = llvm::ConstantArray::get(ArrTy, Constants);
8297
ASTInfo::__brain_cells_ptr = new llvm::GlobalVariable(*M, ArrTy, false,

src/ast/general/ASTInfo.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ class ASTInfo
6161
* @returns The size of the cells used as the Brain's tape.
6262
*/
6363
llvm::GlobalVariable* get_cells_size();
64+
/**
65+
* @returns The type representing the bitsize of each cell (8, 16, 32 or 64 bits).
66+
*/
67+
llvm::Type* get_cell_type(llvm::LLVMContext &C);
68+
/**
69+
* @returns The pointer type representing the bitsize of each
70+
* cell (8, 16, 32 or 64 bits).
71+
*/
72+
llvm::Type* get_cell_ptr_type(llvm::LLVMContext &C);
6473
/**
6574
* @brief Controls if the io.ll module is included within the module which
6675
* is being interpreted, if the module does not uses any function defined in

src/ast/instr/ArithmeticInstr.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ void ArithmeticInstr::code_gen(llvm::Module *M, llvm::IRBuilder<> &B,
1313
llvm::BasicBlock *BreakBB)
1414
{
1515
llvm::Value *IdxV = B.CreateLoad(ASTInfo::instance()->get_index_ptr());
16-
llvm::Value* Idxs[] = { B.getInt32(0), IdxV };
16+
llvm::Value* Idxs[] = { B.getIntN(ArgsOptions::instance()->get_cell_bitsize(), 0),
17+
IdxV };
1718
llvm::ArrayRef<llvm::Value *> IdxsArr(Idxs);
1819
llvm::Value *CellPtr = B.CreateGEP(ASTInfo::instance()->get_cells_ptr(),
1920
IdxsArr);
2021
// Load cell value
2122
llvm::Value *CellV = B.CreateLoad(CellPtr);
2223

2324
// Load index value - 1
24-
llvm::Value *IdxPreV = B.CreateAdd(IdxV, B.getInt32(-1));
25-
llvm::Value* Idxs2[] = { B.getInt32(0), IdxPreV };
25+
llvm::Value *IdxPreV = B.CreateAdd(IdxV,
26+
B.getIntN(ArgsOptions::instance()->get_cell_bitsize(), -1));
27+
llvm::Value* Idxs2[] = { B.getIntN(ArgsOptions::instance()->get_cell_bitsize(), 0),
28+
IdxPreV };
2629
llvm::ArrayRef<llvm::Value *> IdxsArr2(Idxs2);
2730
llvm::Value *CellPtr2 = B.CreateGEP(ASTInfo::instance()->get_cells_ptr(),
2831
IdxsArr2);

src/ast/instr/DebugInstr.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,20 @@ void DebugInstr::code_gen(llvm::Module *M, llvm::IRBuilder<> &B,
1111
llvm::BasicBlock *BreakBB)
1212
{
1313
llvm::LLVMContext &C = M->getContext();
14-
llvm::Type* DebugArgs[] = { llvm::Type::getInt32Ty(C),
15-
llvm::Type::getInt32PtrTy(C),
14+
llvm::Type* DebugArgs[] = { ASTInfo::instance()->get_cell_type(C),
15+
ASTInfo::instance()->get_cell_ptr_type(C),
1616
llvm::Type::getInt32Ty(C) };
1717
llvm::FunctionType *DebugTy = llvm::FunctionType::get(llvm::Type::getVoidTy(C), DebugArgs, false);
18-
llvm::Function *DebugF = llvm::cast<llvm::Function>(M->getOrInsertFunction("b_debug", DebugTy));
18+
llvm::Function *DebugF = llvm::cast<llvm::Function>(
19+
M->getOrInsertFunction(
20+
"b_debug_" + std::to_string(ArgsOptions::instance()->get_cell_bitsize()),
21+
DebugTy
22+
)
23+
);
1924
llvm::Value* Args[] = {
2025
B.CreateLoad(ASTInfo::instance()->get_index_ptr()),
2126
B.CreatePointerCast(ASTInfo::instance()->get_cells_ptr(),
22-
llvm::Type::getInt32Ty(C)->getPointerTo()),
27+
ASTInfo::instance()->get_cell_type(C)->getPointerTo()),
2328
B.CreateLoad(ASTInfo::instance()->get_cells_size())
2429
};
2530
llvm::ArrayRef<llvm::Value *> ArgsArr(Args);

src/ast/instr/FloatInstr.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,26 @@ void FloatInstr::code_gen(llvm::Module *M, llvm::IRBuilder<> &B,
1111
llvm::BasicBlock *BreakBB)
1212
{
1313
llvm::LLVMContext &C = M->getContext();
14-
llvm::Type* PutCharArgs[] = { llvm::Type::getInt32Ty(C),
15-
llvm::Type::getInt32PtrTy(C),
14+
llvm::Type* PutCharArgs[] = { ASTInfo::instance()->get_cell_type(C),
15+
ASTInfo::instance()->get_cell_ptr_type(C),
1616
llvm::Type::getInt32Ty(C) };
1717

1818
llvm::FunctionType *FloatPrintTy =
1919
llvm::FunctionType::get(llvm::Type::getVoidTy(C), PutCharArgs,
2020
false);
21-
llvm::Function *FloatPrintF =
22-
llvm::cast<llvm::Function>(M->getOrInsertFunction("b_float_print",
23-
FloatPrintTy));
21+
llvm::Function *FloatPrintF = llvm::cast<llvm::Function>(
22+
M->getOrInsertFunction(
23+
"b_float_print_" + std::to_string(ArgsOptions::instance()->get_cell_bitsize()),
24+
FloatPrintTy
25+
)
26+
);
2427

2528
// Creating the arguments which will be passed to the called function in
2629
// io.c
2730
llvm::Value* Args[] = {
2831
B.CreateLoad(ASTInfo::instance()->get_index_ptr()),
2932
B.CreatePointerCast(ASTInfo::instance()->get_cells_ptr(),
30-
llvm::Type::getInt32Ty(C)->getPointerTo()),
33+
ASTInfo::instance()->get_cell_type(C)->getPointerTo()),
3134
B.CreateLoad(ASTInfo::instance()->get_cells_size())
3235
};
3336

0 commit comments

Comments
 (0)