Skip to content

Commit

Permalink
add C time funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Nov 22, 2023
1 parent ea47b4a commit 6696153
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. See [Keep a

## Unreleased

### Added

- GDT and GTM (`D` and `T`) now work in compiled programs.

## [1.6.2] - 2023-11-21

### Fixed
Expand Down
8 changes: 6 additions & 2 deletions src/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,14 @@ void compiler::get_c_code(const instruction& i, std::ostream& os, bool assume_as
return;
}
case op::RND:
cerr << "Random number generation is not yet supported for compiled programs." << endl;
exit(EXIT_FAILURE);
case op::GDT:
os << "lws_push(stack, get_date());";
return;
case op::GTM:
cerr << "Nondeterministic instructions are not yet supported for compiled programs." << endl;
exit(EXIT_FAILURE);
os << "lws_push(stack, get_time());";
return;
case op::EXP:
os << "lws_push(stack, 1 << (lws_pop(stack) + 8) >> 8);";
return;
Expand Down
28 changes: 24 additions & 4 deletions src/compiler/lightweight_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ R"(
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>

#ifndef __GNUC__
#define __builtin_expect(exp, c) exp
Expand Down Expand Up @@ -53,7 +54,8 @@ static inline void lws_deinit(const lws stack) {
free(stack->base);
}

#define INVALID_CHAR 0xfffd
static const int32_t INVALID_CHAR = 0xfffd;

// Basically copied from parse_unichar in string_processing.hh
static inline int32_t get_unichar() {
unsigned char buf[4];
Expand Down Expand Up @@ -91,20 +93,38 @@ static inline int32_t get_unichar() {
case 4:
return ((buf[0] & 0x07) << 18) | ((buf[1] & 0x3f) << 12) | ((buf[2] & 0x3f) << 6) | (buf[3] & 0x3f);
}
// Should be unreachable but the compiler doesn't know better
return INVALID_CHAR;
}

static inline void print_unichar(int32_t c) {
if (c <= 0x7f) {
putchar(c);
} else if (c <= 0x07ff) {
char buffer[] = { 0xc0 | (c >> 6), 0x80 | (c & 0x3f), 0 };
printf(buffer);
printf("%s", buffer);
} else if (c <= 0xffff) {
char buffer[] = { 0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f), 0 };
printf(buffer);
printf("%s", buffer);
} else {
char buffer[] = { 0xf0 | (c >> 18), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f), 0 };
printf(buffer);
printf("%s", buffer);
}
}

static const time_t SECS_PER_DAY = 86400;

static inline int32_t get_time() {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
const float UNITS_PER_NSEC = 9.709036e-8F;
const float UNITS_PER_SEC = 97.09036F;
float value = UNITS_PER_SEC * (float)(ts.tv_sec % SECS_PER_DAY) + UNITS_PER_NSEC * (float)ts.tv_nsec;
return (int32_t)value;
}

static inline int32_t get_date() {
time_t t = time(NULL);
return (int32_t)(t / SECS_PER_DAY);
}
//)"
2 changes: 1 addition & 1 deletion trgc.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash

set -euo pipefail
./trilangle -c "$@" | "${CC:-gcc}" -o ./trg.out -xc -
./trilangle -c "$@" | tee out.c | "${CC:-gcc}" -o ./trg.out -xc -
./trg.out

0 comments on commit 6696153

Please sign in to comment.