From 6c1225221bd8a1fdd111057a02814410acc75924 Mon Sep 17 00:00:00 2001 From: asoffer Date: Sat, 18 Nov 2023 14:41:21 -0500 Subject: [PATCH] Support for most of limits.h --- toolchain/stdlib/compat/c/BUILD | 44 +++++++++++++++---- .../stdlib/compat/c/internal/generate.bzl | 2 +- .../compat/c/internal/generate_c_values.cc | 7 ++- toolchain/stdlib/compat/c/limits.template.ic | 19 ++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 toolchain/stdlib/compat/c/limits.template.ic diff --git a/toolchain/stdlib/compat/c/BUILD b/toolchain/stdlib/compat/c/BUILD index 90e2ff97..2cf4377e 100644 --- a/toolchain/stdlib/compat/c/BUILD +++ b/toolchain/stdlib/compat/c/BUILD @@ -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"], @@ -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"], diff --git a/toolchain/stdlib/compat/c/internal/generate.bzl b/toolchain/stdlib/compat/c/internal/generate.bzl index ad8b33de..f96e4717 100644 --- a/toolchain/stdlib/compat/c/internal/generate.bzl +++ b/toolchain/stdlib/compat/c/internal/generate.bzl @@ -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"], diff --git a/toolchain/stdlib/compat/c/internal/generate_c_values.cc b/toolchain/stdlib/compat/c/internal/generate_c_values.cc index b4c01ba3..5f8ce219 100644 --- a/toolchain/stdlib/compat/c/internal/generate_c_values.cc +++ b/toolchain/stdlib/compat/c/internal/generate_c_values.cc @@ -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[]) { diff --git a/toolchain/stdlib/compat/c/limits.template.ic b/toolchain/stdlib/compat/c/limits.template.ic new file mode 100644 index 00000000..c30bfabb --- /dev/null +++ b/toolchain/stdlib/compat/c/limits.template.ic @@ -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}}}