From 2dd89e3b26cbf4c31d47aa4a978ade28914acb24 Mon Sep 17 00:00:00 2001 From: Prashanth Mundkur Date: Thu, 30 Aug 2018 14:05:54 -0700 Subject: Annotate the RISC-V prelude for C builtins. Add some builtins to the C sail lib. Enable some gcc warnings. --- lib/sail.c | 21 +++++++++++++++++++++ lib/sail.h | 6 ++++++ 2 files changed, 27 insertions(+) (limited to 'lib') diff --git a/lib/sail.c b/lib/sail.c index 3c0b0dac..c702455c 100644 --- a/lib/sail.c +++ b/lib/sail.c @@ -132,6 +132,27 @@ void concat_str(sail_string *stro, const sail_string str1, const sail_string str strcat(*stro, str2); } +bool string_startswith(sail_string s, sail_string prefix) +{ + return strstr(s, prefix) == s; +} + +void string_length(sail_int *len, sail_string s) +{ + mpz_set_ui(*len, strlen(s)); +} + +void string_drop(sail_string *dst, sail_string s, sail_int ns) +{ + size_t len = strlen(s); + mach_int n = CREATE_OF(mach_int, sail_int)(ns); + if (len >= n) { + *dst = realloc(*dst, (len - n) + 1); + memcpy(*dst, s + n, len - n); + (*dst)[len - n] = '\0'; + } +} + /* ***** Sail integers ***** */ inline diff --git a/lib/sail.h b/lib/sail.h index 37b6b685..8533cd21 100644 --- a/lib/sail.h +++ b/lib/sail.h @@ -77,6 +77,7 @@ bool eq_string(const sail_string, const sail_string); bool EQUAL(sail_string)(const sail_string, const sail_string); void concat_str(sail_string *stro, const sail_string str1, const sail_string str2); +bool string_startswith(sail_string s, sail_string prefix); /* ***** Sail integers ***** */ @@ -319,6 +320,11 @@ unit prerr_real(const sail_string, const real); void random_real(real *rop, unit); +/* ***** String utilities ***** */ + +void string_length(sail_int *len, sail_string s); +void string_drop(sail_string *dst, sail_string s, sail_int len); + /* ***** Printing ***** */ void string_of_int(sail_string *str, const sail_int i); -- cgit v1.2.3