Skip to content

Commit

Permalink
refactored CONCAT code. Added mod operation support. WIP: report
Browse files Browse the repository at this point in the history
  • Loading branch information
lapotolo committed Feb 26, 2020
1 parent c564203 commit 72b0b03
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
40 changes: 25 additions & 15 deletions src/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,29 +538,20 @@ LLVMValueRef codegen_expr(
LLVMTypeRef array_type_rhs = LLVMGetElementType(LLVMTypeOf(rhs));
unsigned size_rhs = LLVMGetArrayLength(array_type_rhs);

unsigned conc_size = size_lhs + size_rhs;
unsigned size_conc = size_lhs + size_rhs;

LLVMTypeRef conc_elem_type = LLVMGetElementType(array_type_lhs);
LLVMTypeRef conc_vector_type = LLVMArrayType(conc_elem_type, conc_size);
LLVMTypeRef conc_vector_type = LLVMArrayType(conc_elem_type, size_conc);

LLVMValueRef conc_vector_base_address = LLVMBuildAlloca(builder, conc_vector_type, "");

unsigned i = 0;
LLVMValueRef vect_load;
unsigned index_load;
unsigned rhs_index = -1;
while(i < conc_size)
unsigned index_load = 0;
// copy from lhs to the concatenated vector
while(i < size_lhs)
{
if(i < size_lhs) {
vect_load = lhs;
index_load = i;
} else {
++rhs_index;
vect_load = rhs;
index_load = rhs_index;
}
// setup the load from one of the old vector
LLVMValueRef offset_load = LLVMBuildStructGEP(builder, vect_load, index_load, "");
LLVMValueRef offset_load = LLVMBuildStructGEP(builder, lhs, index_load, "");
LLVMValueRef val_to_store = LLVMBuildLoad(builder, offset_load, "");

// compute the offset to store
Expand All @@ -569,6 +560,24 @@ LLVMValueRef codegen_expr(
LLVMValueRef offset_store = LLVMBuildInBoundsGEP2(builder, conc_elem_type, conc_vector_base_address, idxs, 1, "");
// store element i at address: vector_base_address + offset
LLVMBuildStore(builder, val_to_store, offset_store);
++index_load;
++i;
}
index_load = 0;
// copy from rhs to the concatenated vector
while(i < size_conc)
{
// setup the load from one of the old vector
LLVMValueRef offset_load = LLVMBuildStructGEP(builder, rhs, index_load, "");
LLVMValueRef val_to_store = LLVMBuildLoad(builder, offset_load, "");

// compute the offset to store
LLVMValueRef idxs[] = { LLVMConstInt(LLVMInt32Type(), i, 0) };
// compute the offset where the i-th value has to be stored
LLVMValueRef offset_store = LLVMBuildInBoundsGEP2(builder, conc_elem_type, conc_vector_base_address, idxs, 1, "");
// store element i at address: vector_base_address + offset
LLVMBuildStore(builder, val_to_store, offset_store);
++index_load;
++i;
}
return conc_vector_base_address;
Expand All @@ -584,6 +593,7 @@ LLVMValueRef codegen_expr(
case '-': return LLVMBuildSub(builder, lhs, rhs, "");
case '*': return LLVMBuildMul(builder, lhs, rhs, "");
case '/': return LLVMBuildSDiv(builder, lhs, rhs, "");
case MOD: return LLVMBuildURem(builder, lhs, rhs, "");
case '<': return LLVMBuildICmp(builder, LLVMIntSLT, lhs, rhs, "");
case '>': return LLVMBuildICmp(builder, LLVMIntSGT, lhs, rhs, "");
case LE : return LLVMBuildICmp(builder, LLVMIntSLE, lhs, rhs, "");
Expand Down
5 changes: 4 additions & 1 deletion src/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
%token TIMES_KW
// VECT OPs
%token CONCAT_KW
// OTHER OPS
%token MOD

// DEFINE TOKEN TYPES
%type <e> expr
Expand All @@ -50,7 +52,7 @@

%nonassoc ASSIGN_OP
%nonassoc IDENTIFIER
%left CONCAT_KW
%left CONCAT_KW MOD
%left AND_SC AND OR_SC OR
%nonassoc '<' '>' LE GE '=' NE TIMES_KW
%left '+' '-'
Expand Down Expand Up @@ -88,6 +90,7 @@ expr: VAL { $$ = make_val($1); }
| expr '*' expr { $$ = make_bin_op($1, '*', $3); }
| expr '-' expr { $$ = make_bin_op($1, '-', $3); }
| expr '/' expr { $$ = make_bin_op($1, '/', $3); }
| expr MOD expr { $$ = make_bin_op($1, MOD, $3); }

| expr '<' expr { $$ = make_bin_op($1, '<', $3); }
| expr '>' expr { $$ = make_bin_op($1, '>', $3); }
Expand Down
2 changes: 1 addition & 1 deletion src/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ false return LIT_FALSE;
seq return SEQ_KW;
times return TIMES_KW;
\+\+ return CONCAT_KW;

mod return MOD;
[A-Za-z_][A-Za-z_0-9]* { yylval.ident = strdup(yytext); return IDENTIFIER; }
[0-9]+ { yylval.lit_value = atoi(yytext); return VAL; }

Expand Down

0 comments on commit 72b0b03

Please sign in to comment.