Skip to content

Commit

Permalink
Merge pull request #398 from HigherOrderCO/stdbool
Browse files Browse the repository at this point in the history
Use bool type from stdbool.h
  • Loading branch information
developedby authored Jun 26, 2024
2 parents 3b57a21 + 1a36121 commit aa3c64c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 56 deletions.
8 changes: 4 additions & 4 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn compile_book(trg: Target, book: &hvm::Book) -> String {
for (fid, def) in book.defs.iter().enumerate() {
code.push_str(&format!(" case {}: return interact_call_{}(net, tm, a, b);\n", fid, &def.name.replace("/","_").replace(".","_").replace("-","_")));
}
code.push_str(&format!(" default: return FALSE;\n"));
code.push_str(&format!(" default: return false;\n"));
code.push_str(&format!(" }}\n"));
code.push_str(&format!("}}"));

Expand Down Expand Up @@ -70,7 +70,7 @@ pub fn compile_def(trg: Target, code: &mut String, book: &hvm::Book, tab: usize,
code.push_str(&format!(" || !n{:x}", i));
}
code.push_str(&format!(") {{\n"));
code.push_str(&format!("{}return FALSE;\n", indent(tab+2)));
code.push_str(&format!("{}return false;\n", indent(tab+2)));
code.push_str(&format!("{}}}\n", indent(tab+1)));
for i in 0 .. def.vars {
code.push_str(&format!("{}vars_create(net, v{:x}, NONE);\n", indent(tab+1), i));
Expand All @@ -79,7 +79,7 @@ pub fn compile_def(trg: Target, code: &mut String, book: &hvm::Book, tab: usize,
// Allocs resources (using slow allocator)
//code.push_str(&format!("{}// Allocates needed resources.\n", indent(tab+1)));
//code.push_str(&format!("{}if (!get_resources(net, tm, {}, {}, {})) {{\n", indent(tab+1), def.rbag.len()+1, def.node.len(), def.vars));
//code.push_str(&format!("{}return FALSE;\n", indent(tab+2)));
//code.push_str(&format!("{}return false;\n", indent(tab+2)));
//code.push_str(&format!("{}}}\n", indent(tab+1)));
//for i in 0 .. def.node.len() {
//code.push_str(&format!("{}Val n{:x} = tm->nloc[0x{:x}];\n", indent(tab+1), i, i));
Expand All @@ -102,7 +102,7 @@ pub fn compile_def(trg: Target, code: &mut String, book: &hvm::Book, tab: usize,
}

// Return
code.push_str(&format!("{}return TRUE;\n", indent(tab+1)));
code.push_str(&format!("{}return true;\n", indent(tab+1)));
code.push_str(&format!("{}}}\n", indent(tab)));
}

Expand Down
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
Loading

0 comments on commit aa3c64c

Please sign in to comment.