// See LICENSE for license details. #include #include #include #include #include #include #include "util.h" #undef printf #define SYS_write 64 #define ZEROPAD (1<<0) /* Pad with zero */ #define SIGN (1<<1) /* Unsigned/signed long */ #define PLUS (1<<2) /* Show plus */ #define SPACE (1<<3) /* Spacer */ #define LEFT (1<<4) /* Left justified */ #define HEX_PREP (1<<5) /* 0x */ #define UPPERCASE (1<<6) /* 'ABCDEF' */ typedef size_t ee_size_t; #define is_digit(c) ((c) >= '0' && (c) <= '9') /*static ee_size_t strnlen(const char *s, ee_size_t count);*/ #undef strcmp static char *digits = "0123456789abcdefghijklmnopqrstuvwxyz"; static char *upper_digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf); char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf); static void ee_bufcpy(char *d, char *s, int count); extern volatile uint64_t tohost; extern volatile uint64_t fromhost; ee_size_t strnlen(const char *s, ee_size_t count) { const char *sc; for (sc = s; *sc != '\0' && count--; ++sc); return sc - s; } static char *number(char *str, long num, int base, int size, int precision, int type) { char c, sign, tmp[66]; char *dig = digits; int i; if (type & UPPERCASE) dig = upper_digits; if (type & LEFT) type &= ~ZEROPAD; if (base < 2 || base > 36) return 0; c = (type & ZEROPAD) ? '0' : ' '; sign = 0; if (type & SIGN) { if (num < 0) { sign = '-'; num = -num; size--; } else if (type & PLUS) { sign = '+'; size--; } else if (type & SPACE) { sign = ' '; size--; } } if (type & HEX_PREP) { if (base == 16) size -= 2; else if (base == 8) size--; } i = 0; if (num == 0) tmp[i++] = '0'; else { while (num != 0) { tmp[i++] = dig[((unsigned long) num) % (unsigned) base]; num = ((unsigned long) num) / (unsigned) base; } } if (i > precision) precision = i; size -= precision; if (!(type & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' '; if (sign) *str++ = sign; if (type & HEX_PREP) { if (base == 8) *str++ = '0'; else if (base == 16) { *str++ = '0'; *str++ = digits[33]; } } if (!(type & LEFT)) while (size-- > 0) *str++ = c; while (i < precision--) *str++ = '0'; while (i-- > 0) *str++ = tmp[i]; while (size-- > 0) *str++ = ' '; return str; } static char *eaddr(char *str, unsigned char *addr, int size, int precision, int type) { char tmp[24]; char *dig = digits; int i, len; if (type & UPPERCASE) dig = upper_digits; len = 0; for (i = 0; i < 6; i++) { if (i != 0) tmp[len++] = ':'; tmp[len++] = dig[addr[i] >> 4]; tmp[len++] = dig[addr[i] & 0x0F]; } if (!(type & LEFT)) while (len < size--) *str++ = ' '; for (i = 0; i < len; ++i) *str++ = tmp[i]; while (len < size--) *str++ = ' '; return str; } static int skip_atoi(const char **s) { int i = 0; while (is_digit(**s)) i = i*10 + *((*s)++) - '0'; return i; } static char *iaddr(char *str, unsigned char *addr, int size, int precision, int type) { char tmp[24]; int i, n, len; len = 0; for (i = 0; i < 4; i++) { if (i != 0) tmp[len++] = '.'; n = addr[i]; if (n == 0) tmp[len++] = digits[0]; else { if (n >= 100) { tmp[len++] = digits[n / 100]; n = n % 100; tmp[len++] = digits[n / 10]; n = n % 10; } else if (n >= 10) { tmp[len++] = digits[n / 10]; n = n % 10; } tmp[len++] = digits[n]; } } if (!(type & LEFT)) while (len < size--) *str++ = ' '; for (i = 0; i < len; ++i) *str++ = tmp[i]; while (len < size--) *str++ = ' '; return str; } void ee_bufcpy(char *pd, char *ps, int count) { char *pe=ps+count; while (ps!=pe) *pd++=*ps++; } #if HAS_FLOAT static void parse_float(double value, char *buffer, char fmt, int precision) { int decpt, sign, exp, pos; char *digits = NULL; char cvtbuf[80]; int capexp = 0; int magnitude; if (fmt == 'G' || fmt == 'E') { capexp = 1; fmt += 'a' - 'A'; } if (fmt == 'g') { digits = ecvtbuf(value, precision, &decpt, &sign, cvtbuf); magnitude = decpt - 1; if (magnitude < -4 || magnitude > precision - 1) { fmt = 'e'; precision -= 1; } else { fmt = 'f'; precision -= decpt; } } if (fmt == 'e') { digits = ecvtbuf(value, precision + 1, &decpt, &sign, cvtbuf); if (sign) *buffer++ = '-'; *buffer++ = *digits; if (precision > 0) *buffer++ = '.'; ee_bufcpy(buffer, digits + 1, precision); buffer += precision; *buffer++ = capexp ? 'E' : 'e'; if (decpt == 0) { if (value == 0.0) exp = 0; else exp = -1; } else exp = decpt - 1; if (exp < 0) { *buffer++ = '-'; exp = -exp; } else *buffer++ = '+'; buffer[2] = (exp % 10) + '0'; exp = exp / 10; buffer[1] = (exp % 10) + '0'; exp = exp / 10; buffer[0] = (exp % 10) + '0'; buffer += 3; } else if (fmt == 'f') { digits = fcvtbuf(value, precision, &decpt, &sign, cvtbuf); if (sign) *buffer++ = '-'; if (*digits) { if (decpt <= 0) { *buffer++ = '0'; *buffer++ = '.'; for (pos = 0; pos < -decpt; pos++) *buffer++ = '0'; while (*digits) *buffer++ = *digits++; } else { pos = 0; while (*digits) { if (pos++ == decpt) *buffer++ = '.'; *buffer++ = *digits++; } } } else { *buffer++ = '0'; if (precision > 0) { *buffer++ = '.'; for (pos = 0; pos < precision; pos++) *buffer++ = '0'; } } } *buffer = '\0'; } static char *flt(char *str, double num, int size, int precision, char fmt, int flags) { char tmp[80]; char c, sign; int n, i; // Left align means no zero padding if (flags & LEFT) flags &= ~ZEROPAD; // Determine padding and sign char c = (flags & ZEROPAD) ? '0' : ' '; sign = 0; if (flags & SIGN) { if (num < 0.0) { sign = '-'; num = -num; size--; } else if (flags & PLUS) { sign = '+'; size--; } else if (flags & SPACE) { sign = ' '; size--; } } // Compute the precision value if (precision < 0) precision = 6; // Default precision: 6 // Convert floating point number to text parse_float(num, tmp, fmt, precision); if ((flags & HEX_PREP) && precision == 0) decimal_point(tmp); if (fmt == 'g' && !(flags & HEX_PREP)) cropzeros(tmp); n = strnlen(tmp,256); // Output number with alignment and padding size -= n; if (!(flags & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' '; if (sign) *str++ = sign; if (!(flags & LEFT)) while (size-- > 0) *str++ = c; for (i = 0; i < n; i++) *str++ = tmp[i]; while (size-- > 0) *str++ = ' '; return str; } #endif static void decimal_point(char *buffer) { while (*buffer) { if (*buffer == '.') return; if (*buffer == 'e' || *buffer == 'E') break; buffer++; } if (*buffer) { int n = strnlen(buffer,256); while (n > 0) { buffer[n + 1] = buffer[n]; n--; } *buffer = '.'; } else { *buffer++ = '.'; *buffer = '\0'; } } static void cropzeros(char *buffer) { char *stop; while (*buffer && *buffer != '.') buffer++; if (*buffer++) { while (*buffer && *buffer != 'e' && *buffer != 'E') buffer++; stop = buffer--; while (*buffer == '0') buffer--; if (*buffer == '.') buffer--; while (buffer!=stop) *++buffer=0; } } static int ee_vsprintf(char *buf, const char *fmt, va_list args) { int len; unsigned long num; int i, base; char *str; char *s; int flags; // Flags to number() int field_width; // Width of output field int precision; // Min. # of digits for integers; max number of chars for from string int qualifier; // 'h', 'l', or 'L' for integer fields for (str = buf; *fmt; fmt++) { if (*fmt != '%') { *str++ = *fmt; continue; } // Process flags flags = 0; repeat: fmt++; // This also skips first '%' switch (*fmt) { case '-': flags |= LEFT; goto repeat; case '+': flags |= PLUS; goto repeat; case ' ': flags |= SPACE; goto repeat; case '#': flags |= HEX_PREP; goto repeat; case '0': flags |= ZEROPAD; goto repeat; } // Get field width field_width = -1; if (is_digit(*fmt)) field_width = skip_atoi(&fmt); else if (*fmt == '*') { fmt++; field_width = va_arg(args, int); if (field_width < 0) { field_width = -field_width; flags |= LEFT; } } // Get the precision precision = -1; if (*fmt == '.') { ++fmt; if (is_digit(*fmt)) precision = skip_atoi(&fmt); else if (*fmt == '*') { ++fmt; precision = va_arg(args, int); } if (precision < 0) precision = 0; } // Get the conversion qualifier qualifier = -1; if (*fmt == 'l' || *fmt == 'L') { qualifier = *fmt; fmt++; } // Default base base = 10; switch (*fmt) { case 'c': if (!(flags & LEFT)) while (--field_width > 0) *str++ = ' '; *str++ = (unsigned char) va_arg(args, int); while (--field_width > 0) *str++ = ' '; continue; case 's': s = va_arg(args, char *); if (!s) s = ""; len = strnlen(s, precision); if (!(flags & LEFT)) while (len < field_width--) *str++ = ' '; for (i = 0; i < len; ++i) *str++ = *s++; while (len < field_width--) *str++ = ' '; continue; case 'p': if (field_width == -1) { field_width = 2 * sizeof(void *); flags |= ZEROPAD; } str = number(str, (unsigned long) va_arg(args, void *), 16, field_width, precision, flags); continue; case 'A': flags |= UPPERCASE; case 'a': if (qualifier == 'l') str = eaddr(str, va_arg(args, unsigned char *), field_width, precision, flags); else str = iaddr(str, va_arg(args, unsigned char *), field_width, precision, flags); continue; // Integer number formats - set up the flags and "break" case 'o': base = 8; break; case 'X': flags |= UPPERCASE; case 'x': base = 16; break; case 'd': case 'i': flags |= SIGN; case 'u': break; #if HAS_FLOAT case 'f': str = flt(str, va_arg(args, double), field_width, precision, *fmt, flags | SIGN); continue; #endif default: if (*fmt != '%') *str++ = '%'; if (*fmt) *str++ = *fmt; else --fmt; continue; } if (qualifier == 'l') num = va_arg(args, unsigned long); else if (flags & SIGN) num = va_arg(args, int); else num = va_arg(args, unsigned int); str = number(str, num, base, field_width, precision, flags); } *str = '\0'; return str - buf; } static uintptr_t syscall(uintptr_t which, uint64_t arg0, uint64_t arg1, uint64_t arg2) { volatile uint64_t magic_mem[8] __attribute__((aligned(64))); magic_mem[0] = which; magic_mem[1] = arg0; magic_mem[2] = arg1; magic_mem[3] = arg2; __sync_synchronize(); tohost = (uintptr_t)magic_mem; while (fromhost == 0) ; fromhost = 0; __sync_synchronize(); return magic_mem[0]; } #define NUM_COUNTERS 2 static uintptr_t counters[NUM_COUNTERS]; static char* counter_names[NUM_COUNTERS]; void setStats(int enable) { int i = 0; #define READ_CTR(name) do { \ while (i >= NUM_COUNTERS) ; \ uintptr_t csr = read_csr(name); \ if (!enable) { csr -= counters[i]; counter_names[i] = #name; } \ counters[i++] = csr; \ } while (0) READ_CTR(mcycle); READ_CTR(minstret); #undef READ_CTR } /*void __attribute__((noreturn)) tohost_exit(uintptr_t code) { tohost = (code << 1) | 1; while (1); }*/ void __attribute__((noreturn))tohost_exit(uintptr_t code){ tohost=(code<<1)|1; asm ("ecall"); } uintptr_t __attribute__((weak)) handle_trap(uintptr_t cause, uintptr_t epc, uintptr_t regs[32]) { tohost_exit(1337); } void exit(int code) { tohost_exit(code); } void abort() { exit(128 + SIGABRT); } void printstr(const char* s) { syscall(SYS_write, 1, (uintptr_t)s, strlen(s)); } void __attribute__((weak)) thread_entry(int cid, int nc) { // multi-threaded programs override this function. // for the case of single-threaded programs, only let core 0 proceed. while (cid != 0); } int __attribute__((weak)) main(int argc, char** argv) { // single-threaded programs override this function. printstr("Implement main(), foo!\n"); return -1; } static void init_tls() { register void* thread_pointer asm("tp"); extern char _tls_data; extern __thread char _tdata_begin, _tdata_end, _tbss_end; size_t tdata_size = &_tdata_end - &_tdata_begin; memcpy(thread_pointer, &_tls_data, tdata_size); size_t tbss_size = &_tbss_end - &_tdata_end; memset(thread_pointer + tdata_size, 0, tbss_size); } void _init(int cid, int nc) { init_tls(); thread_entry(cid, nc); // only single-threaded programs should ever get here. int ret = main(0, 0); char buf[NUM_COUNTERS * 32] __attribute__((aligned(64))); char* pbuf = buf; for (int i = 0; i < NUM_COUNTERS; i++) if (counters[i]) pbuf += sprintf(pbuf, "%s = %d\n", counter_names[i], counters[i]); if (pbuf != buf) printstr(buf); exit(ret); } #undef putchar int putchar(int ch) { static __thread char buf[64] __attribute__((aligned(64))); static __thread int buflen = 0; buf[buflen++] = ch; if (ch == '\n' || buflen == sizeof(buf)) { syscall(SYS_write, 1, (uintptr_t)buf, buflen); buflen = 0; } return 0; } void printhex(uint64_t x) { char str[17]; int i; for (i = 0; i < 16; i++) { str[15-i] = (x & 0xF) + ((x & 0xF) < 10 ? '0' : 'a'-10); x >>= 4; } str[16] = 0; printstr(str); } static inline void printnum(void (*putch)(int, void**), void **putdat, unsigned long long num, unsigned base, int width, int padc) { unsigned digs[sizeof(num)*CHAR_BIT]; int pos = 0; while (1) { digs[pos++] = num % base; if (num < base) break; num /= base; } while (width-- > pos) putch(padc, putdat); while (pos-- > 0) putch(digs[pos] + (digs[pos] >= 10 ? 'a' - 10 : '0'), putdat); } static unsigned long long getuint(va_list *ap, int lflag) { if (lflag >= 2) return va_arg(*ap, unsigned long long); else if (lflag) return va_arg(*ap, unsigned long); else return va_arg(*ap, unsigned int); } static long long getint(va_list *ap, int lflag) { if (lflag >= 2) return va_arg(*ap, long long); else if (lflag) return va_arg(*ap, long); else return va_arg(*ap, int); } static void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt, va_list ap) { register const char* p; const char* last_fmt; register int ch, err; unsigned long long num; int base, lflag, width, precision, altflag; char padc; while (1) { while ((ch = *(unsigned char *) fmt) != '%') { if (ch == '\0') return; fmt++; putch(ch, putdat); } fmt++; // Process a %-escape sequence last_fmt = fmt; padc = ' '; width = -1; precision = -1; lflag = 0; altflag = 0; reswitch: switch (ch = *(unsigned char *) fmt++) { // flag to pad on the right case '-': padc = '-'; goto reswitch; // flag to pad with 0's instead of spaces case '0': padc = '0'; goto reswitch; // width field case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': for (precision = 0; ; ++fmt) { precision = precision * 10 + ch - '0'; ch = *fmt; if (ch < '0' || ch > '9') break; } goto process_precision; case '*': precision = va_arg(ap, int); goto process_precision; case '.': if (width < 0) width = 0; goto reswitch; case '#': altflag = 1; goto reswitch; process_precision: if (width < 0) width = precision, precision = -1; goto reswitch; // long flag (doubled for long long) case 'l': lflag++; goto reswitch; // character case 'c': putch(va_arg(ap, int), putdat); break; // string case 's': if ((p = va_arg(ap, char *)) == NULL) p = "(null)"; if (width > 0 && padc != '-') for (width -= strnlen(p, precision); width > 0; width--) putch(padc, putdat); for (; (ch = *p) != '\0' && (precision < 0 || --precision >= 0); width--) { putch(ch, putdat); p++; } for (; width > 0; width--) putch(' ', putdat); break; // (signed) decimal case 'd': num = getint(&ap, lflag); if ((long long) num < 0) { putch('-', putdat); num = -(long long) num; } base = 10; goto signed_number; // unsigned decimal case 'u': base = 10; goto unsigned_number; // (unsigned) octal case 'o': // should do something with padding so it's always 3 octits base = 8; goto unsigned_number; // pointer case 'p': static_assert(sizeof(long) == sizeof(void*)); lflag = 1; putch('0', putdat); putch('x', putdat); /* fall through to 'x' */ // (unsigned) hexadecimal case 'X': case 'x': base = 16; unsigned_number: num = getuint(&ap, lflag); signed_number: printnum(putch, putdat, num, base, width, padc); break; // escaped '%' character case '%': putch(ch, putdat); break; // unrecognized escape sequence - just print it literally default: putch('%', putdat); fmt = last_fmt; break; } } } /* int printf(const char* fmt, ...) { va_list ap; va_start(ap, fmt); vprintfmt((void*)putchar, 0, fmt, ap); va_end(ap); return 0; // incorrect return value, but who cares, anyway? }*/ void _send_char(char c) { /*#error "You must implement the method _send_char to use this file!\n"; */ volatile unsigned char *THR=(unsigned char *)0x10000000; volatile unsigned char *LSR=(unsigned char *)0x10000005; while(!(*LSR&0b100000)); *THR=c; while(!(*LSR&0b100000)); } int sprintf(char* str, const char* fmt, ...) { va_list ap; char* str0 = str; va_start(ap, fmt); void sprintf_putch(int ch, void** data) { char** pstr = (char**)data; **pstr = ch; (*pstr)++; } vprintfmt(sprintf_putch, (void**)&str, fmt, ap); *str = 0; va_end(ap); return str - str0; } void* memcpy(void* dest, const void* src, size_t len) { if ((((uintptr_t)dest | (uintptr_t)src | len) & (sizeof(uintptr_t)-1)) == 0) { const uintptr_t* s = src; uintptr_t *d = dest; while (d < (uintptr_t*)(dest + len)) *d++ = *s++; } else { const char* s = src; char *d = dest; while (d < (char*)(dest + len)) *d++ = *s++; } return dest; } void* memset(void* dest, int byte, size_t len) { if ((((uintptr_t)dest | len) & (sizeof(uintptr_t)-1)) == 0) { uintptr_t word = byte & 0xFF; word |= word << 8; word |= word << 16; word |= word << 16 << 16; uintptr_t *d = dest; while (d < (uintptr_t*)(dest + len)){ *d = word; d++;} } else { char *d = dest; while (d < (char*)(dest + len)){ *d = byte; d++;} } return dest; } //recompile pls size_t strlen(const char *s) { const char *p = s; while (*p) p++; return p - s; } /*size_t strnlen(const char *s, size_t n) { const char *p = s; while (n-- && *p) p++; return p - s; }*/ int strcmp(const char* s1, const char* s2) { unsigned char c1, c2; do { c1 = *s1++; c2 = *s2++; } while (c1 != 0 && c1 == c2); return c1 - c2; } char* strcpy(char* dest, const char* src) { char* d = dest; while ((*d++ = *src++)) ; return dest; } long atol(const char* str) { long res = 0; int sign = 0; while (*str == ' ') str++; if (*str == '-' || *str == '+') { sign = *str == '-'; str++; } while (*str) { res *= 10; res += *str++ - '0'; } return sign ? -res : res; } int sendstring(const char *p){ int n=0; while (*p) { _send_char(*p); n++; p++; } return n; } int gg_printf(const char *fmt, ...) { char buf[256],*p; va_list args; int n=0; va_start(args, fmt); ee_vsprintf(buf, fmt, args); va_end(args); p=buf; /* while (*p) { _send_char(*p); n++; p++; } */ n=sendstring(p); return n; } int puts(const char* s) { gg_printf(s); gg_printf("\n"); return 0; // incorrect return value, but who cares, anyway? } unsigned long getTimer(void){ unsigned long *MTIME = (unsigned long*)0x0200BFF8; return *MTIME; }