Skip to content

Commit

Permalink
Support for most of limits.h
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Nov 18, 2023
1 parent 02e89ba commit 6c12252
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
44 changes: 36 additions & 8 deletions toolchain/stdlib/compat/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,48 @@ ic_library(
srcs = [":generate_c_types"],
)

genrule(
name = "generate_c_types",
outs = ["types.ic"],
cmd = "./$(location //toolchain/stdlib/compat/c/internal:c_types_generator) > $@",
tools = ["//toolchain/stdlib/compat/c/internal:c_types_generator"],
visibility = ["//visibility:private"],
)

ic_library(
name = "ctype",
srcs = ["ctype.ic"],
deps = [":types"],
)

generated_ic_library(
name = "limits",
srcs = ["limits.template.ic"],
includes = ["limits.h"],
symbols = {
"CHAR_BIT": "int",
"SCHAR_MIN": "int",
"SCHAR_MAX": "int",
"UCHAR_MAX": "int",
"CHAR_MIN": "int",
"CHAR_MAX": "int",
"MB_LEN_MAX": "int",
"SHRT_MIN": "int",
"SHRT_MAX": "int",
"USHRT_MAX": "unsigned int",
"INT_MIN": "int",
"INT_MAX": "int",
"UINT_MAX": "unsigned int",
"LONG_MIN": "long",
"LONG_MAX": "long",
"ULONG_MAX": "unsigned long",
"LLONG_MIN": "long long",
"LLONG_MAX": "long long",
"ULLONG_MAX": "unsigned long long",
},
deps = [],
)

ic_library(
name = "stdio",
srcs = ["stdio.ic"],
Expand All @@ -26,14 +62,6 @@ ic_library(
deps = [":types"],
)

genrule(
name = "generate_c_types",
outs = ["types.ic"],
cmd = "./$(location //toolchain/stdlib/compat/c/internal:c_types_generator) > $@",
tools = ["//toolchain/stdlib/compat/c/internal:c_types_generator"],
visibility = ["//visibility:private"],
)

generated_ic_library(
name = "time",
srcs = ["time.template.ic"],
Expand Down
2 changes: 1 addition & 1 deletion toolchain/stdlib/compat/c/internal/generate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def generated_ic_library(name, **kwargs):
outs = ["internal_generate_c_values_{}_src.cc".format(name)],
cmd = "./$(location //toolchain/stdlib/compat/c/internal:generate_c_values) \"{}\" {} > $@".format(
"".join(["#include <{}>\n".format(i) for i in kwargs["includes"]]),
" ".join(["{} {}".format(*kv) for kv in kwargs["symbols"].items()])
" ".join(["{} \"{}\"".format(*kv) for kv in kwargs["symbols"].items()])
),
tools = ["//toolchain/stdlib/compat/c/internal:generate_c_values"],
visibility = ["//visibility:private"],
Expand Down
7 changes: 6 additions & 1 deletion toolchain/stdlib/compat/c/internal/generate_c_values.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

char const *FormatSpecifier(char const *type) {
if (std::strcmp(type, "int") == 0) { return "d"; }
if (std::strcmp(type, "long") == 0) { return "ld"; }
if (std::strcmp(type, "long long") == 0) { return "lld"; }
if (std::strcmp(type, "unsigned int") == 0) { return "u"; }
if (std::strcmp(type, "unsigned long") == 0) { return "lu"; }
if (std::strcmp(type, "unsigned long long") == 0) { return "llu"; }
if (std::strcmp(type, "char") == 0) { return "c"; }
if (std::strcmp(type, "double") == 0) { return "f"; }
if (std::strcmp(type, "float") == 0) { return "f"; }
std::abort();
return "WTF";
}

int main(int argc, char const *argv[]) {
Expand Down
19 changes: 19 additions & 0 deletions toolchain/stdlib/compat/c/limits.template.ic
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
let CHAR_BIT ::= {{{CHAR_BIT}}}
// let SCHAR_MIN ::= {{{SCHAR_MIN}}} TODO: Enable when negation is support
let SCHAR_MAX ::= {{{SCHAR_MAX}}}
let UCHAR_MAX ::= {{{UCHAR_MAX}}}
// let CHAR_MIN ::= builtin.ascii_encode({{{CHAR_MIN}}}) TODO: Enable when negation is support
let CHAR_MAX ::= builtin.ascii_encode({{{CHAR_MAX}}})
let MB_LEN_MAX ::= {{{MB_LEN_MAX}}}
// let SHRT_MIN ::= {{{SHRT_MIN}}} TODO: Enable when negation is support
let SHRT_MAX ::= {{{SHRT_MAX}}}
let USHRT_MAX ::= {{{USHRT_MAX}}}
// let INT_MIN ::= {{{INT_MIN}}} TODO: Enable when negation is support
let INT_MAX ::= {{{INT_MAX}}}
let UINT_MAX ::= {{{UINT_MAX}}}
// let LONG_MIN ::= {{{LONG_MIN}}} TODO: Enable when negation is support
let LONG_MAX ::= {{{LONG_MAX}}}
let ULONG_MAX ::= {{{ULONG_MAX}}}
// let LLONG_MIN ::= {{{LLONG_MIN}}} TODO: Enable when negation is support
let LLONG_MAX ::= {{{LLONG_MAX}}}
let ULLONG_MAX ::= {{{ULLONG_MAX}}}

0 comments on commit 6c12252

Please sign in to comment.