Skip to content

Commit

Permalink
Use bool type from stdbool.h
Browse files Browse the repository at this point in the history
  • Loading branch information
developedby committed Jun 26, 2024
1 parent 3b57a21 commit 32d9883
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 52 deletions.
71 changes: 33 additions & 38 deletions src/hvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <math.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -16,15 +17,9 @@
#define INTERPRETED
#define WITHOUT_MAIN

// Booleans
#define TRUE 1
#define FALSE 0

// Types
// --------

typedef uint8_t bool;

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
Expand Down Expand Up @@ -251,7 +246,7 @@ bool get_par_flag(Pair pair) {
if (get_tag(p1) == REF) {
return (get_val(p1) >> 28) == 1;
} else {
return FALSE;
return false;
}
}

Expand Down Expand Up @@ -716,7 +711,7 @@ static inline Net* net_new() {
// ---------

u32 node_alloc_1(Net* net, TM* tm, u32* lps) {
while (TRUE) {
while (true) {
u32 lc = tm->tid*(G_NODE_LEN/TPC) + (tm->nput%(G_NODE_LEN/TPC));
Pair elem = net->node_buf[lc];
tm->nput += 1;
Expand All @@ -729,7 +724,7 @@ u32 node_alloc_1(Net* net, TM* tm, u32* lps) {
}

u32 vars_alloc_1(Net* net, TM* tm, u32* lps) {
while (TRUE) {
while (true) {
u32 lc = tm->tid*(G_NODE_LEN/TPC) + (tm->vput%(G_NODE_LEN/TPC));
Port elem = net->vars_buf[lc];
tm->vput += 1;
Expand Down Expand Up @@ -816,7 +811,7 @@ static inline Port enter(Net* net, Port var) {
// Atomically Links `A ~ B`.
static inline void link(Net* net, TM* tm, Port A, Port B) {
// Attempts to directionally point `A ~> B`
while (TRUE) {
while (true) {
// If `A` is NODE: swap `A` and `B`, and continue
if (get_tag(A) != VAR && get_tag(B) == VAR) {
Port X = A; A = B; B = X;
Expand Down Expand Up @@ -858,13 +853,13 @@ static inline bool interact_link(Net* net, TM* tm, Port a, Port b) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, 1, 0, 0)) {
debug("interact_link: get_resources failed\n");
return FALSE;
return false;
}

// Links.
link_pair(net, tm, new_pair(a, b));

return TRUE;
return true;
}

// Declared here for use in call interactions.
Expand All @@ -887,7 +882,7 @@ static inline bool interact_call(Net* net, TM* tm, Port a, Port b, Book* book) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, def->rbag_len + 1, def->node_len, def->vars_len)) {
debug("interact_call: get_resources failed\n");
return FALSE;
return false;
}

// Stores new vars.
Expand All @@ -906,26 +901,26 @@ static inline bool interact_call(Net* net, TM* tm, Port a, Port b, Book* book) {
}
link_pair(net, tm, new_pair(adjust_port(net, tm, def->root), b));

return TRUE;
return true;
}
#endif

// The Void Interaction.
static inline bool interact_void(Net* net, TM* tm, Port a, Port b) {
return TRUE;
return true;
}

// The Eras Interaction.
static inline bool interact_eras(Net* net, TM* tm, Port a, Port b) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, 2, 0, 0)) {
debug("interact_eras: get_resources failed\n");
return FALSE;
return false;
}

// Checks availability
if (node_load(net, get_val(b)) == 0) {
return FALSE;
return false;
}

// Loads ports.
Expand All @@ -937,20 +932,20 @@ static inline bool interact_eras(Net* net, TM* tm, Port a, Port b) {
link_pair(net, tm, new_pair(a, B1));
link_pair(net, tm, new_pair(a, B2));

return TRUE;
return true;
}

// The Anni Interaction.
static inline bool interact_anni(Net* net, TM* tm, Port a, Port b) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, 2, 0, 0)) {
debug("interact_anni: get_resources failed\n");
return FALSE;
return false;
}

// Checks availability
if (node_load(net, get_val(a)) == 0 || node_load(net, get_val(b)) == 0) {
return FALSE;
return false;
}

// Loads ports.
Expand All @@ -965,20 +960,20 @@ static inline bool interact_anni(Net* net, TM* tm, Port a, Port b) {
link_pair(net, tm, new_pair(A1, B1));
link_pair(net, tm, new_pair(A2, B2));

return TRUE;
return true;
}

// The Comm Interaction.
static inline bool interact_comm(Net* net, TM* tm, Port a, Port b) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, 4, 4, 4)) {
debug("interact_comm: get_resources failed\n");
return FALSE;
return false;
}

// Checks availability
if (node_load(net, get_val(a)) == 0 || node_load(net, get_val(b)) == 0) {
return FALSE;
return false;
}

// Loads ports.
Expand Down Expand Up @@ -1007,20 +1002,20 @@ static inline bool interact_comm(Net* net, TM* tm, Port a, Port b) {
link_pair(net, tm, new_pair(new_port(get_tag(a), tm->nloc[2]), B1));
link_pair(net, tm, new_pair(new_port(get_tag(a), tm->nloc[3]), B2));

return TRUE;
return true;
}

// The Oper Interaction.
static inline bool interact_oper(Net* net, TM* tm, Port a, Port b) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, 1, 1, 0)) {
debug("interact_oper: get_resources failed\n");
return FALSE;
return false;
}

// Checks availability
if (node_load(net, get_val(b)) == 0) {
return FALSE;
return false;
}

// Loads ports.
Expand All @@ -1039,20 +1034,20 @@ static inline bool interact_oper(Net* net, TM* tm, Port a, Port b) {
link_pair(net, tm, new_pair(B1, new_port(OPR, tm->nloc[0])));
}

return TRUE;
return true;
}

// The Swit Interaction.
static inline bool interact_swit(Net* net, TM* tm, Port a, Port b) {
// Allocates needed nodes and vars.
if (!get_resources(net, tm, 1, 2, 0)) {
debug("interact_swit: get_resources failed\n");
return FALSE;
return false;
}

// Checks availability
if (node_load(net, get_val(b)) == 0) {
return FALSE;
return false;
}

// Loads ports.
Expand All @@ -1071,7 +1066,7 @@ static inline bool interact_swit(Net* net, TM* tm, Port a, Port b) {
link_pair(net, tm, new_pair(new_port(CON, tm->nloc[0]), B1));
}

return TRUE;
return true;
}

// Pops a local redex and performs a single interaction.
Expand Down Expand Up @@ -1116,14 +1111,14 @@ static inline bool interact(Net* net, TM* tm, Book* book) {
// If error, pushes redex back.
if (!success) {
push_redex(net, tm, redex);
return FALSE;
return false;
// Else, increments the interaction count.
} else if (rule != LINK) {
tm->itrs += 1;
}
}

return TRUE;
return true;
}

// Evaluator
Expand All @@ -1137,14 +1132,14 @@ void evaluator(Net* net, TM* tm, Book* book) {
// Performs some interactions
u32 tick = 0;
bool busy = tm->tid == 0;
while (TRUE) {
while (true) {
tick += 1;

// If we have redexes...
if (rbag_len(net, tm) > 0) {
// Update global idle counter
if (!busy) atomic_fetch_sub_explicit(&net->idle, 1, memory_order_relaxed);
busy = TRUE;
busy = true;

// Perform an interaction
#ifdef DEBUG
Expand All @@ -1156,7 +1151,7 @@ void evaluator(Net* net, TM* tm, Book* book) {
} else {
// Update global idle counter
if (busy) atomic_fetch_add_explicit(&net->idle, 1, memory_order_relaxed);
busy = FALSE;
busy = false;

//// Peeks a redex from target
u32 sid = (tm->tid - 1) % TPC;
Expand Down Expand Up @@ -1436,12 +1431,12 @@ bool book_load(Book* book, u32* buf) {

if (def->rbag_len > DEF_RBAG_LEN) {
fprintf(stderr, "def '%s' has too many redexes: %u\n", def->name, def->rbag_len);
return FALSE;
return false;
}

if (def->node_len > DEF_NODE_LEN) {
fprintf(stderr, "def '%s' has too many nodes: %u\n", def->name, def->node_len);
return FALSE;
return false;
}

// Reads root
Expand All @@ -1456,7 +1451,7 @@ bool book_load(Book* book, u32* buf) {
buf += def->node_len * 2;
}

return TRUE;
return true;
}

// Debug Printing
Expand Down
9 changes: 3 additions & 6 deletions src/hvm.cu
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ typedef float f32;
typedef double f64;
typedef unsigned long long int u64;

#define FALSE false
#define TRUE true

// Configuration
// -------------

Expand Down Expand Up @@ -1917,12 +1914,12 @@ bool book_load(Book* book, u32* buf) {

if (def->rbag_len > L_NODE_LEN/TPB) {
fprintf(stderr, "def '%s' has too many redexes: %u\n", def->name, def->rbag_len);
return FALSE;
return false;
}

if (def->node_len > L_NODE_LEN/TPB) {
fprintf(stderr, "def '%s' has too many nodes: %u\n", def->name, def->node_len);
return FALSE;
return false;
}

// Reads root
Expand All @@ -1937,7 +1934,7 @@ bool book_load(Book* book, u32* buf) {
buf += def->node_len * 2;
}

return TRUE;
return true;
}

// Debug Printing
Expand Down
1 change: 0 additions & 1 deletion src/hvm.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// Types
// -----

typedef uint8_t bool;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
Expand Down
2 changes: 1 addition & 1 deletion src/hvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include <math.h>
#include <stdint.h>
#include <stdbool.h>

// Types
// -----

typedef uint8_t bool;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
Expand Down
6 changes: 3 additions & 3 deletions src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Ctr readback_ctr(Net* net, Book* book, Port port) {
ctr.tag = get_u24(get_val(arg_port));

// Loads remaining arguments
while (TRUE) {
while (true) {
app_port = expand(net, book, get_snd(app_node));
if (get_tag(app_port) != CON) break;
app_node = node_load(net, get_val(app_port));
Expand Down Expand Up @@ -107,7 +107,7 @@ Bytes readback_bytes(Net* net, Book* book, Port port) {
bytes.len = 0;

// Readback loop
while (TRUE) {
while (true) {
// Normalizes the net
normalize(net, book);

Expand Down Expand Up @@ -590,7 +590,7 @@ void do_run_io(Net* net, Book* book, Port port) {
setlinebuf(stderr);

// IO loop
while (TRUE) {
while (true) {
// Normalizes the net
normalize(net, book);

Expand Down
6 changes: 3 additions & 3 deletions src/run.cu
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Ctr gnet_readback_ctr(GNet* gnet, Port port) {
ctr.tag = get_u24(get_val(arg_port));

// Loads remaining arguments
while (TRUE) {
while (true) {
app_port = gnet_expand(gnet, get_snd(app_node));
if (get_tag(app_port) != CON) break;
app_node = gnet_node_load(gnet, get_val(app_port));
Expand Down Expand Up @@ -109,7 +109,7 @@ extern "C" Bytes gnet_readback_bytes(GNet* gnet, Port port) {
bytes.len = 0;

// Readback loop
while (TRUE) {
while (true) {
// Normalizes the net
gnet_normalize(gnet);

Expand Down Expand Up @@ -626,7 +626,7 @@ void do_run_io(GNet* gnet, Book* book, Port port) {
setlinebuf(stderr);

// IO loop
while (TRUE) {
while (true) {
// Normalizes the net
gnet_normalize(gnet);

Expand Down

0 comments on commit 32d9883

Please sign in to comment.