Skip to content

Commit

Permalink
#236 small revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Oct 16, 2021
1 parent 0ce1fa0 commit ede1155
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
37 changes: 21 additions & 16 deletions ravicomp/src/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2734,7 +2734,7 @@ static void walk_node(C_Code_Analysis *analysis, C_Node *node)
}
}

static int analyze_C_code(Function *fn, TextBuffer *C_code)
static int analyze_C_code(Function *fn, TextBuffer *user_code)
{
static const char* addition_decls = "\n"
"TValue ival0;\n"
Expand All @@ -2747,20 +2747,21 @@ static int analyze_C_code(Function *fn, TextBuffer *C_code)
"TValue fval2;\n"
"TValue bval2;\n"
;
TextBuffer code;
raviX_buffer_init(&code, 1024);
raviX_buffer_add_string(&code, Embedded_C_header);
raviX_buffer_add_string(&code, addition_decls);
TextBuffer canned_code;
raviX_buffer_init(&canned_code, 1024);
// setup types and symbols that are needed to check the canned_code
raviX_buffer_add_string(&canned_code, Embedded_C_header); // Dummy types and functions
raviX_buffer_add_string(&canned_code, addition_decls); // Dummy variables
if (fn->proc->linearizer->C_declarations.buf)
raviX_buffer_add_string(&code, fn->proc->linearizer->C_declarations.buf);
raviX_buffer_add_string(&canned_code, fn->proc->linearizer->C_declarations.buf);
if (fn->C_local_declarations.buf) /* declarations of temp integer and float vars */
raviX_buffer_add_string(&code, fn->C_local_declarations.buf);
raviX_buffer_add_string(&canned_code, fn->C_local_declarations.buf);

C_Code_Analysis analysis = {0};
C_Parser parser;
C_parser_init(&parser);
C_Scope *global_scope = C_global_scope(&parser);
C_Token *tok = C_tokenize_buffer(&parser, code.buf);
C_Token *tok = C_tokenize_buffer(&parser, canned_code.buf);
if (tok == NULL) {
analysis.status = -1;
goto Lexit;
Expand All @@ -2771,12 +2772,15 @@ static int analyze_C_code(Function *fn, TextBuffer *C_code)
goto Lexit;
}

tok = C_tokenize_buffer(&parser, C_code->buf);
/* Now parse the user supplied code */
tok = C_tokenize_buffer(&parser, user_code->buf);
if (tok == NULL){
analysis.status = -1;
goto Lexit;
}
C_convert_pp_tokens(&parser, tok);
/* Note user supplied code is parsed as compound statement in global scope - i.e.
* not inside a function! */
parser.embedded_mode = true;
C_Node *node = C_parse_compound_statement(global_scope, &parser, tok);
if (node == NULL){
Expand All @@ -2790,7 +2794,7 @@ static int analyze_C_code(Function *fn, TextBuffer *C_code)
fn->api->error_message(fn->api->context, parser.error_message);
}
C_parser_destroy(&parser);
raviX_buffer_free(&code);
raviX_buffer_free(&canned_code);

return analysis.status;
}
Expand Down Expand Up @@ -2880,7 +2884,10 @@ static void emit_userdata_C_variable_store(Function *fn, Instruction *insn, Pseu
raviX_buffer_add_string(&fn->body, " }\n");
}


/* Outputs the C__unsafe statements. Note that C__decl statements are
* output at top level so that declarations are shared amongst all
* functions in a chunk of Ravi code.
*/
static int emit_op_embed_C(Function *fn, Instruction *insn)
{
// Save the buffer and switch to new one temporarily
Expand All @@ -2899,16 +2906,14 @@ static int emit_op_embed_C(Function *fn, Instruction *insn)
// output C code
Pseudo *C_code = get_first_target(insn);
assert(C_code->type == PSEUDO_CONSTANT && C_code->constant->type == RAVI_TSTRING);
raviX_buffer_add_string(&fn->body, " {\n");
raviX_buffer_add_string(&fn->body, C_code->constant->s->str);
raviX_buffer_add_string(&fn->body, " }\n");

// Store values back to Ravi/Lua variables
for (int i = 0; i < get_num_operands(insn); i++) {
Pseudo *pseudo = get_operand(insn, i);
LuaSymbol *symbol = pseudo->type == PSEUDO_SYMBOL ? pseudo->symbol : pseudo->temp_for_local;
if (symbol->variable.value_type.type_code == RAVI_TNUMINT ||
symbol->variable.value_type.type_code == RAVI_TNUMFLT) {
emit_userdata_C_variable_store(fn, insn, pseudo);
}
emit_userdata_C_variable_store(fn, insn, pseudo);
}

raviX_buffer_add_string(&fn->body, "\n}\n");
Expand Down
11 changes: 7 additions & 4 deletions ravicomp/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ static AstNode *parse_primary_expression(ParserState *parser)
return primary_expr;
}

/* Parse C__new '(' string ',' expr ')' */
static AstNode *parse_builtin_expression(ParserState *parser)
{
LexerState *ls = parser->ls;
Expand Down Expand Up @@ -1216,9 +1217,11 @@ static AstNode *parse_goto_statment(ParserState *parser)
return goto_stmt;
}

/* Parse C__unsafe and C__decl */
static AstNode *parse_embedded_C(ParserState *parser, bool is_decl) {
LexerState *ls = parser->ls;
/* stat -> C (NAME {',' NAME}) string */
/* stat -> C__unsafe (NAME {',' NAME}) string */
/* stat -> C__decl string */
AstNode *node = raviX_allocate_ast_node(parser, STMT_EMBEDDED_C);
node->embedded_C_stmt.C_src_snippet = NULL;
node->embedded_C_stmt.symbols = NULL;
Expand All @@ -1237,7 +1240,7 @@ static AstNode *parse_embedded_C(ParserState *parser, bool is_decl) {
if (symbol && is_local)
raviX_add_symbol(parser->container, &node->embedded_C_stmt.symbols, symbol);
else {
raviX_syntaxerror(ls, "Argument must be local variable");
raviX_syntaxerror(ls, "Argument to C__unsafe() must be a local variable");
}
raviX_next(ls);
while (testnext(ls, ',')) {
Expand All @@ -1246,14 +1249,14 @@ static AstNode *parse_embedded_C(ParserState *parser, bool is_decl) {
if (symbol && is_local)
raviX_add_symbol(parser->container, &node->embedded_C_stmt.symbols, symbol);
else {
raviX_syntaxerror(ls, "Argument must be local variable");
raviX_syntaxerror(ls, "Argument to C__unsafe() must be a local variable");
}
}
checknext(ls, ')');
break;
}
default: {
raviX_syntaxerror(ls, "Expected set of arguments");
raviX_syntaxerror(ls, "Expected set of arguments for C__unsafe()");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/comptests/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function runfile(file)
f()
end

--runfile("inputs/53_ravi_tests.lua")
runfile("inputs/53_ravi_tests.lua")
runfile("inputs/54_embed_C.lua")
runfile("inputs/55_embed_C.lua")

Expand Down

0 comments on commit ede1155

Please sign in to comment.