Skip to content

Commit

Permalink
#236 Embedded C initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Oct 16, 2021
1 parent 02ad8d4 commit 0ce1fa0
Show file tree
Hide file tree
Showing 33 changed files with 6,636 additions and 21 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ cmake-build-release
buildnojit
nojit
nojita
*.so
*.so
*.dll
*.exp
*.lib
*.obj
*.s
time-debug.txt
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/******************************************************************************
* Copyright (C) 1994-2019 Lua.org, PUC-Rio.
* Portions Copyright (C) 2015-2020 Dibyendu Majumdar
* Portions Copyright (C) 2015-2021 Dibyendu Majumdar
* Portions Copyright (c) 2019 Rui Ueyama
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
Expand Down
14 changes: 12 additions & 2 deletions ravicomp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ set(HEADERS
src/common.h
src/optimizer.h
src/parser.h
src/codegen.h)
src/codegen.h
src/chibicc/chibicc.h)

set(SRCS
src/allocate.c
Expand All @@ -43,6 +44,13 @@ set(SRCS
src/membuf.c
src/codegen.c
src/ravi_binding.c
src/chibicc/chibicc_tokenize.c
src/chibicc/chibicc_parse.c
src/chibicc/chibicc_type.c
src/chibicc/chibicc_strings.c
src/chibicc/chibicc_unicode.c
src/chibicc/chibicc_hashmap.c
../src/ravi_alloc.c
)

message("SOURCE dir is ${RaviCompiler_SOURCE_DIR}")
Expand Down Expand Up @@ -88,7 +96,9 @@ add_library(ravicomp ${LIBTYPE}
target_include_directories(ravicomp
PUBLIC "${CMAKE_CURRENT_BINARY_DIR}"
PUBLIC "${RaviCompiler_SOURCE_DIR}/include"
PRIVATE "${RaviCompiler_SOURCE_DIR}/src")
PRIVATE "${RaviCompiler_SOURCE_DIR}/src"
PRIVATE "${RaviCompiler_SOURCE_DIR}/../src"
)
target_link_libraries(ravicomp ${EXTRA_LIBRARIES})
set_property(TARGET ravicomp PROPERTY C_STANDARD 99)
include(GenerateExportHeader)
Expand Down
1 change: 1 addition & 0 deletions ravicomp/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ MIT License

Copyright (c) 2019-2021 Dibyendu Majumdar
Portions Copyright (c) 1994–2019 Lua.org, PUC-Rio.
Portions Copyright (c) 2019 Rui Ueyama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
15 changes: 14 additions & 1 deletion ravicomp/include/ravi_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ enum TokenType {
TOK_in,
TOK_local,
TOK_defer,
TOK_C__decl,
TOK_C__unsafe,
TOK_C__new,
TOK_nil,
TOK_not,
TOK_or,
Expand Down Expand Up @@ -299,6 +302,7 @@ enum AstNodeType {
STMT_FOR_NUM,
STMT_REPEAT,
STMT_EXPR, /* Also used for assignment statements */
STMT_EMBEDDED_C,
EXPR_LITERAL,
EXPR_SYMBOL,
EXPR_Y_INDEX, /* [] operator */
Expand All @@ -310,7 +314,8 @@ enum AstNodeType {
EXPR_FUNCTION, /* function literal */
EXPR_TABLE_LITERAL, /* table constructor */
EXPR_FUNCTION_CALL,
EXPR_CONCAT
EXPR_CONCAT,
EXPR_BUILTIN
};

typedef struct Statement Statement;
Expand All @@ -325,6 +330,7 @@ typedef struct TestThenStatement TestThenStatement;
typedef struct IfStatement IfStatement;
typedef struct WhileOrRepeatStatement WhileOrRepeatStatement;
typedef struct ForStatement ForStatement;
typedef struct EmbeddedCStatement EmbeddedCStatement;

typedef struct Expression Expression;
typedef struct LiteralExpression LiteralExpression;
Expand All @@ -338,6 +344,7 @@ typedef struct TableLiteralExpression TableLiteralExpression;
typedef struct SuffixedExpression SuffixedExpression;
typedef struct FunctionCallExpression FunctionCallExpression;
typedef struct StringConcatenationExpression StringConcatenationExpression;
typedef struct BuiltinExpression BuiltinExpression;

typedef struct Scope Scope;

Expand Down Expand Up @@ -446,6 +453,11 @@ RAVICOMP_EXPORT void raviX_for_statement_body_foreach_statement(const ForStateme
void (*callback)(void *userdata,
const Statement *statement));

RAVICOMP_EXPORT void raviX_embedded_C_statement_foreach_symbol(const EmbeddedCStatement *statement, void *userdata,
void (*callback)(void *,
const LuaVariableSymbol *expr));
RAVICOMP_EXPORT const StringObject *raviX_embedded_C_statement_C_source(const EmbeddedCStatement *statement);

/* literal expression */
/* Note: '...' value has type RAVI_TVARARGS and no associated SemInfo. */
RAVICOMP_EXPORT const VariableType *raviX_literal_expression_type(const LiteralExpression *expression);
Expand Down Expand Up @@ -541,6 +553,7 @@ RAVICOMP_EXPORT const TestThenStatement *raviX_test_then_statement(const Stateme
RAVICOMP_EXPORT const IfStatement *raviX_if_statement(const Statement *stmt);
RAVICOMP_EXPORT const WhileOrRepeatStatement *raviX_while_or_repeat_statement(const Statement *stmt);
RAVICOMP_EXPORT const ForStatement *raviX_for_statement(const Statement *stmt);
RAVICOMP_EXPORT const EmbeddedCStatement *raviX_embedded_C_statment(const Statement *stmt);

/* Convert an expression to the correct type */
RAVICOMP_EXPORT enum AstNodeType raviX_expression_type(const Expression *expression);
Expand Down
2 changes: 1 addition & 1 deletion ravicomp/src/allocate.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ extern size_t raviX_del_array_element(void *p, size_t element_size, size_t array

#endif

#endif
#endif
4 changes: 4 additions & 0 deletions ravicomp/src/ast_lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ static void process_expression(CompilerState *container, AstNode *node)
case EXPR_TABLE_LITERAL:
process_expression_list(container, node->table_expr.expr_list);
break;
case EXPR_BUILTIN:
break;
default:
assert(0);
break;
Expand Down Expand Up @@ -369,6 +371,8 @@ static void process_statement(CompilerState *container, AstNode *node)
case STMT_FOR_IN:
lower_for_in_statement(container, node);
break;
case STMT_EMBEDDED_C:
break;
default:
fprintf(stderr, "AST = %d\n", node->type);
assert(0);
Expand Down
17 changes: 16 additions & 1 deletion ravicomp/src/ast_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ static void printf_buf(TextBuffer *buf, const char *format, ...)
type = va_arg(ap, const VariableType *);
if (type->type_code == RAVI_TUSERDATA) {
const StringObject *s = type->type_name;
raviX_buffer_add_string(buf, s->str);
if (s != NULL)
raviX_buffer_add_string(buf, s->str);
else
raviX_buffer_add_string(buf, "userdata");
} else {
raviX_buffer_add_string(buf, raviX_get_type_name(type->type_code));
}
Expand Down Expand Up @@ -463,6 +466,12 @@ void raviX_print_ast_node(TextBuffer *buf, AstNode *node, int level)
printf_buf(buf, "%pend\n", level);
break;
}
case STMT_EMBEDDED_C: {
printf_buf(buf, "%pC (\n", level);
print_symbol_list(buf, node->embedded_C_stmt.symbols, level + 1, ",");
printf_buf(buf, "%p ) '%t'\n", level, node->embedded_C_stmt.C_src_snippet);
break;
}
case EXPR_SUFFIXED: {
printf_buf(buf, "%p%c %T\n", level, "[suffixed expr start]", &node->suffixed_expr.type);
printf_buf(buf, "%p%c %T\n", level + 1, "[primary start]",
Expand Down Expand Up @@ -575,6 +584,12 @@ void raviX_print_ast_node(TextBuffer *buf, AstNode *node, int level)
printf_buf(buf, "%p%c\n", level, "[concat end]");
break;
}
case EXPR_BUILTIN: {
printf_buf(buf, "%p%s %c%T\n", level, "C__new(", "", &node->builtin_expr.type);
// TODO print contents
printf_buf(buf, "%p)\n", level);
break;
}
default:
printf_buf(buf, "%pUnsupported node type %d\n", level, node->type);
assert(0);
Expand Down
4 changes: 4 additions & 0 deletions ravicomp/src/ast_simplify.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ static void process_expression(CompilerState *container, AstNode *node)
case EXPR_TABLE_LITERAL:
process_expression_list(container, node->table_expr.expr_list);
break;
case EXPR_BUILTIN:
break;
default:
assert(0);
break;
Expand Down Expand Up @@ -566,6 +568,8 @@ static void process_statement(CompilerState *container, AstNode *node)
process_expression_list(container, node->for_stmt.expr_list);
process_statement_list(container, node->for_stmt.for_statement_list);
break;
case STMT_EMBEDDED_C:
break;
default:
fprintf(stderr, "AST = %d\n", node->type);
assert(0);
Expand Down
21 changes: 21 additions & 0 deletions ravicomp/src/ast_walker.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,27 @@ const ForStatement *raviX_for_statement(const Statement *stmt)
assert(stmt->type == STMT_FOR_IN || stmt->type == STMT_FOR_NUM);
return &n(stmt)->for_stmt;
}
const EmbeddedCStatement *raviX_embedded_C_statment(const Statement *stmt)
{
assert(stmt->type == STMT_EMBEDDED_C);
return &n(stmt)->embedded_C_stmt;
}
void raviX_embedded_C_statement_foreach_symbol(const EmbeddedCStatement *statement, void *userdata,
void (*callback)(void *, const LuaVariableSymbol *expr))
{
LuaSymbol *symbol;
FOR_EACH_PTR(statement->symbols, LuaSymbol, symbol)
{
assert(symbol->symbol_type == SYM_LOCAL);
callback(userdata, &symbol->variable);
}
END_FOR_EACH_PTR(symbol)
}
const StringObject *raviX_embedded_C_statement_C_source(const EmbeddedCStatement *statement)
{
return statement->C_src_snippet;
}

enum AstNodeType raviX_expression_type(const Expression *expression) { return expression->type; }
const LiteralExpression *raviX_literal_expression(const Expression *expr)
{
Expand Down
7 changes: 7 additions & 0 deletions ravicomp/src/chibicc/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
BasedOnStyle: LLVM
IndentWidth: 2
UseTab: Never
BreakBeforeBraces: Attach
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 120
7 changes: 7 additions & 0 deletions ravicomp/src/chibicc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This is a modified version of https://github.com/rui314/chibicc.

```
MIT License
Copyright (c) 2019 Rui Ueyama
```
Loading

0 comments on commit 0ce1fa0

Please sign in to comment.