Skip to content

Translate-C changes #3984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src-self-hosted/clang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub const struct_ZigClangImplicitCastExpr = @OpaqueType();
pub const struct_ZigClangIncompleteArrayType = @OpaqueType();
pub const struct_ZigClangIntegerLiteral = @OpaqueType();
pub const struct_ZigClangMacroDefinitionRecord = @OpaqueType();
pub const struct_ZigClangMacroExpansion = @OpaqueType();
pub const struct_ZigClangMacroQualifiedType = @OpaqueType();
pub const struct_ZigClangMemberExpr = @OpaqueType();
pub const struct_ZigClangNamedDecl = @OpaqueType();
Expand Down Expand Up @@ -889,6 +890,7 @@ pub const ZigClangImplicitCastExpr = struct_ZigClangImplicitCastExpr;
pub const ZigClangIncompleteArrayType = struct_ZigClangIncompleteArrayType;
pub const ZigClangIntegerLiteral = struct_ZigClangIntegerLiteral;
pub const ZigClangMacroDefinitionRecord = struct_ZigClangMacroDefinitionRecord;
pub const ZigClangMacroExpansion = struct_ZigClangMacroExpansion;
pub const ZigClangMacroQualifiedType = struct_ZigClangMacroQualifiedType;
pub const ZigClangMemberExpr = struct_ZigClangMemberExpr;
pub const ZigClangNamedDecl = struct_ZigClangNamedDecl;
Expand Down Expand Up @@ -1128,3 +1130,5 @@ pub extern fn ZigClangCompoundAssignOperator_getBeginLoc(*const ZigClangCompound
pub extern fn ZigClangCompoundAssignOperator_getOpcode(*const ZigClangCompoundAssignOperator) ZigClangBO;
pub extern fn ZigClangCompoundAssignOperator_getLHS(*const ZigClangCompoundAssignOperator) *const ZigClangExpr;
pub extern fn ZigClangCompoundAssignOperator_getRHS(*const ZigClangCompoundAssignOperator) *const ZigClangExpr;

pub extern fn ZigClangMacroExpansion_getDefinition(*const ZigClangMacroExpansion) *const ZigClangMacroDefinitionRecord;
8 changes: 8 additions & 0 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,15 @@ struct AstNodeFloatLiteral {
bool overflow;
};

enum IntLiteralFormat {
IntLiteralFormatNone,
IntLiteralFormatHex,
IntLiteralFormatOctal,
};

struct AstNodeIntLiteral {
BigInt *bigint;
IntLiteralFormat format = IntLiteralFormatNone;
};

struct AstNodeStructValueField {
Expand Down Expand Up @@ -2129,6 +2136,7 @@ struct CodeGen {
bool verbose_llvm_ir;
bool verbose_cimport;
bool verbose_cc;
bool quiet_translate_c;
bool error_during_imports;
bool generate_error_name_table;
bool enable_cache; // mutually exclusive with output_dir
Expand Down
17 changes: 15 additions & 2 deletions src/ast_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,21 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
{
Buf rendered_buf = BUF_INIT;
buf_resize(&rendered_buf, 0);
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 10);
fprintf(ar->f, "%s", buf_ptr(&rendered_buf));

switch (node->data.int_literal.format) {
case IntLiteralFormatHex:
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 16);
fprintf(ar->f, "0x%s", buf_ptr(&rendered_buf));
break;
case IntLiteralFormatOctal:
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 8);
fprintf(ar->f, "0o%s", buf_ptr(&rendered_buf));
break;
case IntLiteralFormatNone:
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 10);
fprintf(ar->f, "%s", buf_ptr(&rendered_buf));
break;
}
}
break;
case NodeTypeStringLiteral:
Expand Down
58 changes: 58 additions & 0 deletions src/c_tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static void begin_token(CTokenize *ctok, CTokId id) {
break;
case CTokIdCharLit:
case CTokIdNumLitFloat:
case CTokIdPlus:
case CTokIdMinus:
case CTokIdLParen:
case CTokIdRParen:
Expand All @@ -125,7 +126,11 @@ static void begin_token(CTokenize *ctok, CTokId id) {
case CTokIdBang:
case CTokIdTilde:
case CTokIdShl:
case CTokIdShr:
case CTokIdLt:
case CTokIdGt:
case CTokIdInc:
case CTokIdDec:
break;
}
}
Expand Down Expand Up @@ -229,6 +234,10 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
begin_token(ctok, CTokIdLt);
ctok->state = CTokStateGotLt;
break;
case '>':
begin_token(ctok, CTokIdGt);
ctok->state = CTokStateGotGt;
break;
case '(':
begin_token(ctok, CTokIdLParen);
end_token(ctok);
Expand All @@ -241,8 +250,13 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
begin_token(ctok, CTokIdAsterisk);
end_token(ctok);
break;
case '+':
begin_token(ctok, CTokIdPlus);
ctok->state = CTokStateGotPlus;
break;
case '-':
begin_token(ctok, CTokIdMinus);
ctok->state = CTokStateGotMinus;
end_token(ctok);
break;
case '!':
Expand Down Expand Up @@ -270,6 +284,45 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
continue;
}
break;
case CTokStateGotGt:
switch (*c) {
case '>':
ctok->cur_tok->id = CTokIdShr;
end_token(ctok);
ctok->state = CTokStateStart;
break;
default:
end_token(ctok);
ctok->state = CTokStateStart;
continue;
}
break;
case CTokStateGotPlus:
switch (*c) {
case '+':
ctok->cur_tok->id = CTokIdInc;
end_token(ctok);
ctok->state = CTokStateStart;
break;
default:
end_token(ctok);
ctok->state = CTokStateStart;
continue;
}
break;
case CTokStateGotMinus:
switch (*c) {
case '-':
ctok->cur_tok->id = CTokIdDec;
end_token(ctok);
ctok->state = CTokStateStart;
break;
default:
end_token(ctok);
ctok->state = CTokStateStart;
continue;
}
break;
case CTokStateFloat:
switch (*c) {
case '.':
Expand Down Expand Up @@ -375,6 +428,7 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
case 'x':
case 'X':
ctok->state = CTokStateHex;
ctok->cur_tok->data.num_lit_int.format = CNumLitFormatHex;
break;
case '.':
ctok->state = CTokStateFloat;
Expand All @@ -391,6 +445,7 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
default:
c -= 1;
ctok->state = CTokStateOctal;
ctok->cur_tok->data.num_lit_int.format = CNumLitFormatOctal;
continue;
}
break;
Expand Down Expand Up @@ -811,6 +866,9 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
case CTokStateNumLitIntSuffixUL:
case CTokStateNumLitIntSuffixLL:
case CTokStateGotLt:
case CTokStateGotGt:
case CTokStateGotPlus:
case CTokStateGotMinus:
end_token(ctok);
break;
case CTokStateFloat:
Expand Down
15 changes: 15 additions & 0 deletions src/c_tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum CTokId {
CTokIdNumLitInt,
CTokIdNumLitFloat,
CTokIdSymbol,
CTokIdPlus,
CTokIdMinus,
CTokIdLParen,
CTokIdRParen,
Expand All @@ -26,7 +27,11 @@ enum CTokId {
CTokIdBang,
CTokIdTilde,
CTokIdShl,
CTokIdShr,
CTokIdLt,
CTokIdGt,
CTokIdInc,
CTokIdDec,
};

enum CNumLitSuffix {
Expand All @@ -38,9 +43,16 @@ enum CNumLitSuffix {
CNumLitSuffixLLU,
};

enum CNumLitFormat {
CNumLitFormatNone,
CNumLitFormatHex,
CNumLitFormatOctal,
};

struct CNumLitInt {
uint64_t x;
CNumLitSuffix suffix;
CNumLitFormat format;
};

struct CTok {
Expand Down Expand Up @@ -81,6 +93,9 @@ enum CTokState {
CTokStateNumLitIntSuffixLL,
CTokStateNumLitIntSuffixUL,
CTokStateGotLt,
CTokStateGotGt,
CTokStateGotPlus,
CTokStateGotMinus,
};

struct CTokenize {
Expand Down
9 changes: 7 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
" --verbose-llvm-ir enable compiler debug output for LLVM IR\n"
" --verbose-cimport enable compiler debug output for C imports\n"
" --verbose-cc enable compiler debug output for C compilation\n"
" --quiet-translate-c disable translate C warnings\n"
" -dirafter [dir] add directory to AFTER include search path\n"
" -isystem [dir] add directory to SYSTEM include search path\n"
" -I[dir] add directory to include search path\n"
Expand Down Expand Up @@ -479,6 +480,7 @@ int main(int argc, char **argv) {
bool verbose_llvm_ir = false;
bool verbose_cimport = false;
bool verbose_cc = false;
bool quiet_translate_c = false;
ErrColor color = ErrColorAuto;
CacheOpt enable_cache = CacheOptAuto;
Buf *dynamic_linker = nullptr;
Expand Down Expand Up @@ -692,6 +694,8 @@ int main(int argc, char **argv) {
verbose_cimport = true;
} else if (strcmp(arg, "--verbose-cc") == 0) {
verbose_cc = true;
} else if (strcmp(arg, "--quiet-translate-c") == 0) {
quiet_translate_c = true;
} else if (strcmp(arg, "-rdynamic") == 0) {
rdynamic = true;
} else if (strcmp(arg, "--each-lib-rpath") == 0) {
Expand Down Expand Up @@ -886,7 +890,7 @@ int main(int argc, char **argv) {
} else if (strcmp(arg, "--linker-script") == 0) {
linker_script = argv[i];
} else if (strcmp(arg, "--version-script") == 0) {
version_script = buf_create_from_str(argv[i]);
version_script = buf_create_from_str(argv[i]);
} else if (strcmp(arg, "-target-glibc") == 0) {
target_glibc = argv[i];
} else if (strcmp(arg, "-rpath") == 0) {
Expand Down Expand Up @@ -1212,7 +1216,7 @@ int main(int argc, char **argv) {
codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
g->want_single_threaded = want_single_threaded;
codegen_set_linker_script(g, linker_script);
g->version_script_path = version_script;
g->version_script_path = version_script;
if (each_lib_rpath)
codegen_set_each_lib_rpath(g, each_lib_rpath);

Expand All @@ -1228,6 +1232,7 @@ int main(int argc, char **argv) {
g->verbose_llvm_ir = verbose_llvm_ir;
g->verbose_cimport = verbose_cimport;
g->verbose_cc = verbose_cc;
g->quiet_translate_c = quiet_translate_c;
g->output_dir = output_dir;
g->disable_gen_h = disable_gen_h;
g->bundle_compiler_rt = bundle_compiler_rt;
Expand Down
Loading