Skip to content

Commit

Permalink
Implement assert.h using PDCLib
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed Jan 8, 2024
1 parent 7385ba3 commit 887ed60
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 36 deletions.
3 changes: 3 additions & 0 deletions mos-platform/common/c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# C standard library and C++ abi.
add_platform_library(common-c
# assert.h
assert.c

# ctype.h
ctype.c

Expand Down
17 changes: 17 additions & 0 deletions mos-platform/common/c/assert.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <assert.h>

#include <stdio.h>
#include <stdlib.h>

void __assert(const char *file, const char *line, const char *function,
const char *expr) {
fputs(file, stderr);
fputc(':', stderr);
fputs(line, stderr);
fputs(": ", stderr);
fputs(function, stderr);
fputs(": assertion failed: ", stderr);
fputs(expr, stderr);
fputc('\n', stderr);
abort();
}
16 changes: 14 additions & 2 deletions mos-platform/common/c/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@ int putchar(int c) {
// included in LTO.
__attribute((interrupt, no_isr)) int puts(const char *s) {
for (; *s; ++s)
__putchar(*s);
__putchar('\n');
putchar(*s);
putchar('\n');
return 0;
}

FILE *stdin;
FILE *stdout;
FILE *stderr;

int fputc(int c, FILE *stream) { return putchar(c); }

int fputs(const char *__restrict__ s, FILE *__restrict__ stream) {
for (; *s; ++s)
putchar(*s);
return 0;
}
9 changes: 9 additions & 0 deletions mos-platform/common/include/__internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __INTERNAL_H_
#define __INTERNAL_H_

// Originally from the Public Domain C Library (PDCLib).

#define _SYMBOL_TO_STRING(x) #x
#define _VALUE_TO_STRING(x) _SYMBOL_TO_STRING(x)

#endif // not __INTERNAL_H_
30 changes: 30 additions & 0 deletions mos-platform/common/include/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Originally from the Public Domain C library (PDCLib).

#ifdef __cplusplus
extern "C" {
#endif

#include <__internal.h>

#ifndef _ASSERT_H
#define _ASSERT_H
void __assert(const char *file, const char *line, const char *function,
const char *expr);
#endif

/* If NDEBUG is set, assert() is a null operation. */
#undef assert

#ifdef NDEBUG
#define assert(ignore) ((void)0)
#else
// Breaking this apart by component saves space.
#define assert(expression) \
((expression) ? (void)0 \
: __assert(__FILE__, _VALUE_TO_STRING(__LINE__), __func__, \
#expression))
#endif

#ifdef __cplusplus
}
#endif
21 changes: 21 additions & 0 deletions mos-platform/common/include/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ int getchar(void);
// To be defined by platform.
void __putchar(char c);

// Originally from the Public Domain C Library (PDCLib).

struct _FILE;
typedef struct _FILE FILE;

extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;

/**
* Write the value c (cast to unsigned char) to the given stream.
* \return c if successful, EOF otherwise.
*/
int fputc(int c, FILE *stream);

/**
* Write the string s (not including the terminating \0) to the given stream.
* \return A value >=0 if successful, EOF otherwise.
*/
int fputs(const char *__restrict__ s, FILE *__restrict__ stream);

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 1 addition & 2 deletions mos-platform/sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ endif()

include_directories(BEFORE SYSTEM .)

install(FILES assert.h stdlib.h TYPE INCLUDE)
install(FILES stdlib.h TYPE INCLUDE)

add_platform_library(sim-crt0)
merge_libraries(sim-crt0
Expand All @@ -16,7 +16,6 @@ merge_libraries(sim-crt0
)

add_platform_library(sim-c
assert.c
putchar.c
stdlib.c
sim-io.c
Expand Down
8 changes: 0 additions & 8 deletions mos-platform/sim/assert.c

This file was deleted.

24 changes: 0 additions & 24 deletions mos-platform/sim/assert.h

This file was deleted.

0 comments on commit 887ed60

Please sign in to comment.