NJU-ProjectN/abstract-machine ics2023 initialized

NJU-ProjectN/abstract-machine 3348db971fd860be5cb28e21c18f9d0e65d0c96a Merge pull request #8 from Jasonyanyusong/master
This commit is contained in:
xinyangli 2023-12-21 00:20:42 +08:00
parent 2824efad33
commit 8e4feb4010
129 changed files with 9017 additions and 0 deletions

View file

@ -0,0 +1,39 @@
#ifndef KLIB_MACROS_H__
#define KLIB_MACROS_H__
#define ROUNDUP(a, sz) ((((uintptr_t)a) + (sz) - 1) & ~((sz) - 1))
#define ROUNDDOWN(a, sz) ((((uintptr_t)a)) & ~((sz) - 1))
#define LENGTH(arr) (sizeof(arr) / sizeof((arr)[0]))
#define RANGE(st, ed) (Area) { .start = (void *)(st), .end = (void *)(ed) }
#define IN_RANGE(ptr, area) ((area).start <= (ptr) && (ptr) < (area).end)
#define STRINGIFY(s) #s
#define TOSTRING(s) STRINGIFY(s)
#define _CONCAT(x, y) x ## y
#define CONCAT(x, y) _CONCAT(x, y)
#define putstr(s) \
({ for (const char *p = s; *p; p++) putch(*p); })
#define io_read(reg) \
({ reg##_T __io_param; \
ioe_read(reg, &__io_param); \
__io_param; })
#define io_write(reg, ...) \
({ reg##_T __io_param = (reg##_T) { __VA_ARGS__ }; \
ioe_write(reg, &__io_param); })
#define static_assert(const_cond) \
static char CONCAT(_static_assert_, __LINE__) [(const_cond) ? 1 : -1] __attribute__((unused))
#define panic_on(cond, s) \
({ if (cond) { \
putstr("AM Panic: "); putstr(s); \
putstr(" @ " __FILE__ ":" TOSTRING(__LINE__) " \n"); \
halt(1); \
} })
#define panic(s) panic_on(1, s)
#endif

View file

@ -0,0 +1,58 @@
#ifndef KLIB_H__
#define KLIB_H__
#include <am.h>
#include <stddef.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
//#define __NATIVE_USE_KLIB__
// string.h
void *memset (void *s, int c, size_t n);
void *memcpy (void *dst, const void *src, size_t n);
void *memmove (void *dst, const void *src, size_t n);
int memcmp (const void *s1, const void *s2, size_t n);
size_t strlen (const char *s);
char *strcat (char *dst, const char *src);
char *strcpy (char *dst, const char *src);
char *strncpy (char *dst, const char *src, size_t n);
int strcmp (const char *s1, const char *s2);
int strncmp (const char *s1, const char *s2, size_t n);
// stdlib.h
void srand (unsigned int seed);
int rand (void);
void *malloc (size_t size);
void free (void *ptr);
int abs (int x);
int atoi (const char *nptr);
// stdio.h
int printf (const char *format, ...);
int sprintf (char *str, const char *format, ...);
int snprintf (char *str, size_t size, const char *format, ...);
int vsprintf (char *str, const char *format, va_list ap);
int vsnprintf (char *str, size_t size, const char *format, va_list ap);
// assert.h
#ifdef NDEBUG
#define assert(ignore) ((void)0)
#else
#define assert(cond) \
do { \
if (!(cond)) { \
printf("Assertion fail at %s:%d\n", __FILE__, __LINE__); \
halt(1); \
} \
} while (0)
#endif
#ifdef __cplusplus
}
#endif
#endif