Skip to content

Commit

Permalink
Generate types.ic module
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Nov 18, 2023
1 parent 2ea4417 commit ab38d3c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
1 change: 1 addition & 0 deletions bazel/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def _impl(ctx):
"-fPIE",
]),
linking_flags([
"-ldl",
"-lffi",
"-lm",
"-lstdc++",
Expand Down
2 changes: 1 addition & 1 deletion toolchain/ic_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _ic_compile_impl(ctx):
inputs = depset([src_file, mod_file], transitive = icm_deps),
outputs = [icm_file],
arguments = [
src_file.short_path,
src_file.path,
"--output={}".format(icm_file.path),
"--module-map={}".format(mod_file.path),
# "--debug-parser=true",
Expand Down
10 changes: 9 additions & 1 deletion toolchain/stdlib/compat/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ load("//toolchain:ic_rule.bzl", "ic_library")

package(default_visibility = ["//visibility:public"])

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 = "types",
srcs = ["types.ic"],
srcs = [":generate_c_types"],
)

ic_library(
Expand Down
6 changes: 6 additions & 0 deletions toolchain/stdlib/compat/c/internal/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//toolchain/stdlib/compat/c:__subpackages__"])

cc_binary(
name = "c_types_generator",
srcs = ["c_types_generator.cc"],
)
31 changes: 31 additions & 0 deletions toolchain/stdlib/compat/c/internal/c_types_generator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <climits>
#include <cstdio>
#include <type_traits>

template <typename T>
void PrintIntegerDeclaration(char const *name) {
std::printf("let %-*s ::= %c%lu\n", 18, name,
std::is_signed<T>::value ? 'i' : 'u', sizeof(T) * CHAR_BIT);
}

template <typename T>
void PrintFloatingPointDeclaration(char const *name) {
std::printf("let %-*s ::= f%lu\n", 18, name, sizeof(T) * CHAR_BIT);
}

int main() {
PrintIntegerDeclaration<signed char>("signed_char");
PrintIntegerDeclaration<unsigned char>("unsigned_char");
PrintIntegerDeclaration<signed short>("short");
PrintIntegerDeclaration<unsigned short>("unsigned_short");
PrintIntegerDeclaration<signed int>("int");
PrintIntegerDeclaration<unsigned int>("unsigned_int");
PrintIntegerDeclaration<signed long>("long");
PrintIntegerDeclaration<unsigned long>("unsigned_long");
PrintIntegerDeclaration<signed long long>("long_long");
PrintIntegerDeclaration<unsigned long long>("unsigned_long_long");
PrintFloatingPointDeclaration<float>("float");
PrintFloatingPointDeclaration<double>("double");

return 0;
}
2 changes: 1 addition & 1 deletion toolchain/stdlib/compat/c/string.ic
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

let c ::= import "std.compat.c.types"

let size_t ::= c.size_t
let size_t ::= c.unsigned_long

let strcpy ::= builtin.foreign("strcpy", ([*]char, [*]char) -> [*]char)
let strncpy ::= builtin.foreign("strncpy", ([*]char, [*]char, size_t) -> [*]char)
Expand Down
2 changes: 1 addition & 1 deletion toolchain/stdlib/compat/c/time.ic
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let c ::= import "std.compat.c.types"
let clock_t ::= c.long
let time_t ::= c.long
let tm ::= builtin.opaque()
let size_t ::= c.size_t
let size_t ::= c.unsigned_long

let CLOCKS_PER_SEC :: clock_t = 1000000

Expand Down
14 changes: 0 additions & 14 deletions toolchain/stdlib/compat/c/types.ic

This file was deleted.

0 comments on commit ab38d3c

Please sign in to comment.