issue #98 Start work generating linear IR

parser
Dibyendu Majumdar 4 years ago
parent a68643dca7
commit 7b06b524ca

@ -56,7 +56,8 @@ if ($ENV{CLION_IDE})
endif()
# define the Lua core source files
set(RAVI_AST_SOURCES src/ravi_ast_parse.c src/ravi_ast_print.c src/ravi_ast_typecheck.c src/ravi_ast_linearize.c)
set(RAVI_AST_SOURCES src/ravi_ast_parse.c src/ravi_ast_print.c src/ravi_ast_typecheck.c src/ravi_ast_linearize.c
src/ravi_fnv_hash.c src/ravi_hash_table.c src/ravi_set.c src/ravi_int_set.c)
set(LUA_CORE_SRCS src/lapi.c src/lcode.c src/lctype.c src/ldebug.c src/ldo.c src/ldump.c
src/lfunc.c src/lgc.c src/llex.c src/lmem.c src/lobject.c src/lopcodes.c
src/lparser.c src/lstate.c src/lstring.c src/ltable.c src/ltm.c src/lundump.c

@ -23,6 +23,7 @@ b) Perform type checking (Ravi enhancement)
#include "ravi_ast.h"
#include "ravi_membuf.h"
#include "ravi_set.h"
#include "allocate.h"
#include "ptrlist.h"
@ -91,6 +92,7 @@ struct lua_symbol {
struct {
const TString *var_name; /* name of the variable */
struct block_scope *block; /* NULL if global symbol, as globals are never added to a scope */
void *backend_data; /* Place for backend to hold some data for the symbol */
} var;
struct {
const TString *label_name;
@ -303,6 +305,7 @@ enum opcode { OP_NOP };
/* pseudo represents a pseudo (virtual) register */
struct pseudo {
unsigned regnum : 16;
struct lua_symbol *symbol; /* If local var this should be set */
};
/* single instruction */
@ -332,6 +335,7 @@ struct basic_block {
#define CFG_FIELDS \
unsigned node_count; \
unsigned allocated; \
struct node **nodes; \
struct node *entry; \
struct node *exit
@ -340,12 +344,35 @@ struct cfg {
CFG_FIELDS;
};
struct pseudo_generator {
uint8_t next_reg;
int16_t free_pos;
uint8_t free_regs[256];
};
struct constant {
uint8_t type;
uint16_t index; /* index number starting from 0 assigned to each constant - acts like a reg num */
union {
lua_Integer i;
lua_Number n;
const TString *s;
};
};
/* proc is a type of cfg */
struct proc {
CFG_FIELDS;
struct proc_list* procs; /* procs defined in this proc */
struct proc* parent; /* enclosing proc */
struct ast_node* function_expr; /* function ast that we are compiling */
struct block_scope *current_scope;
struct pseudo_generator local_pseudos; /* locals */
struct pseudo_generator temp_int_pseudos; /* temporaries known to be integer type */
struct pseudo_generator temp_flt_pseudos; /* temporaries known to be number type */
struct pseudo_generator temp_pseudos; /* All other temporaries */
struct set *constants;
unsigned num_constants;
};
static inline struct basic_block * n2bb(struct node *n) { return (struct basic_block *)n; }
@ -359,6 +386,8 @@ struct linearizer {
struct allocator ptrlist_allocator;
struct allocator basic_block_allocator;
struct allocator proc_allocator;
struct allocator unsized_allocator;
struct allocator constant_allocator;
struct ast_container *ast_container;
struct proc* main_proc; /* The root of the compiled chunk of code */
struct proc_list *all_procs; /* All procs allocated by the linearizer */

@ -0,0 +1,45 @@
/*
* Copyright © 2009 Intel Corporation
* Copyright © 2014 Broadcom
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*/
/* Quick FNV-1 hash implementation based on:
* http://www.isthe.com/chongo/tech/comp/fnv/
*/
#include <inttypes.h>
uint32_t fnv1_hash_string(const char *key);
uint32_t fnv1_hash_data(const void *data, size_t size);
int string_key_equals(const void *a, const void *b);
#define hash_table_create_for_string() \
hash_table_create((uint32_t (*)(const void *key))fnv1_hash_string, \
string_key_equals)
#define set_create_for_string() \
set_create((uint32_t (*)(const void *key))fnv1_hash_string, \
string_key_equals)

@ -0,0 +1,109 @@
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#ifndef RAVI_HASH_TABLE_H
#define RAVI_HASH_TABLE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <inttypes.h>
struct hash_entry {
uint32_t hash;
const void *key;
void *data;
};
struct hash_table {
struct hash_entry *table;
uint32_t (*hash_function)(const void *key);
int (*key_equals_function)(const void *a, const void *b);
uint32_t size;
uint32_t rehash;
uint32_t max_entries;
uint32_t size_index;
uint32_t entries;
uint32_t deleted_entries;
};
struct hash_table *
hash_table_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b));
void
hash_table_destroy(struct hash_table *ht,
void (*delete_function)(struct hash_entry *entry));
struct hash_entry *
hash_table_insert(struct hash_table *ht, const void *key, void *data);
struct hash_entry *
hash_table_search(struct hash_table *ht, const void *key);
void
hash_table_remove(struct hash_table *ht, const void *key);
void
hash_table_remove_entry(struct hash_table *ht, struct hash_entry *entry);
struct hash_entry *
hash_table_next_entry(struct hash_table *ht,
struct hash_entry *entry);
struct hash_entry *
hash_table_random_entry(struct hash_table *ht,
int (*predicate)(struct hash_entry *entry));
/**
* This foreach function is safe against deletion (which just replaces
* an entry's data with the deleted marker), but not against insertion
* (which may rehash the table, making entry a dangling pointer).
*/
#define hash_table_foreach(ht, entry) \
for (entry = hash_table_next_entry(ht, NULL); \
entry != NULL; \
entry = hash_table_next_entry(ht, entry))
/* Alternate interfaces to reduce repeated calls to hash function. */
struct hash_entry *
hash_table_search_pre_hashed(struct hash_table *ht,
uint32_t hash,
const void *key);
struct hash_entry *
hash_table_insert_pre_hashed(struct hash_table *ht,
uint32_t hash,
const void *key, void *data);
#ifdef __cplusplus
} /* extern C */
#endif
#endif

@ -0,0 +1,82 @@
/*
* Copyright © 2009,2013 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#ifndef RAVI_INT_SET_H
#define RAVI_INT_SET_H
#include <inttypes.h>
#include <stdbool.h>
struct int_set_entry {
uint32_t value;
unsigned int occupied : 1;
unsigned int deleted : 1;
};
struct int_set {
struct int_set_entry *table;
uint32_t size;
uint32_t rehash;
uint32_t max_entries;
uint32_t size_index;
uint32_t entries;
uint32_t deleted_entries;
};
struct int_set *
int_set_create(void);
void
int_set_destroy(struct int_set *set);
struct int_set_entry *
int_set_add(struct int_set *set, uint32_t value);
bool
int_set_contains(struct int_set *set, uint32_t value);
void
int_set_remove(struct int_set *set, uint32_t value);
struct int_set_entry *
int_set_search(struct int_set *set, uint32_t value);
void
int_set_remove_entry(struct int_set *set, struct int_set_entry *entry);
struct int_set_entry *
int_set_next_entry(struct int_set *set, struct int_set_entry *entry);
/* Return a random entry in the set that satisfies predicate.
*
* The 'predicate' function pointer may be NULL in which any random
* entry will be returned. */
struct int_set_entry *
int_set_random_entry(struct int_set *set,
int (*predicate)(struct int_set_entry *entry));
#endif

@ -0,0 +1,98 @@
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*
*/
#ifndef RAVI_SET_H
#define RAVI_SET_H
#include <inttypes.h>
#include <stdbool.h>
struct set_entry {
uint32_t hash;
const void *key;
};
struct set {
struct set_entry *table;
uint32_t (*hash_function)(const void *key);
int (*key_equals_function)(const void *a, const void *b);
uint32_t size;
uint32_t rehash;
uint32_t max_entries;
uint32_t size_index;
uint32_t entries;
uint32_t deleted_entries;
};
struct set *
set_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b));
void
set_destroy(struct set *set,
void (*delete_function)(struct set_entry *entry));
struct set_entry *
set_add(struct set *set, const void *key);
bool
set_contains(struct set *set, const void *key);
void
set_remove(struct set *set, const void *key);
struct set_entry *
set_search(struct set *set, const void *key);
void
set_remove_entry(struct set *set, struct set_entry *entry);
struct set_entry *
set_next_entry(struct set *set, struct set_entry *entry);
struct set_entry *
set_random_entry(struct set *set,
int (*predicate)(struct set_entry *entry));
/**
* This foreach function is safe against deletion (which just replaces
* an entry's data with the deleted marker), but not against insertion
* (which may rehash the table, making entry a dangling pointer).
*/
#define set_foreach(ht, entry) \
for (entry = set_next_entry(ht, NULL); \
entry != NULL; \
entry = set_next_entry(ht, entry))
/* Alternate interfaces to reduce repeated calls to hash function. */
struct set_entry *
set_search_pre_hashed(struct set *set, uint32_t hash, const void *key);
struct set_entry *
set_add_pre_hashed(struct set *set, uint32_t hash, const void *key);
#endif

@ -2,12 +2,30 @@
Copyright (C) 2018-2020 Dibyendu Majumdar
*/
#include <ravi_ast.h>
#include <ptrlist.h>
#include "ravi_ast.h"
#include "ptrlist.h"
#include "ravi_fnv_hash.h"
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <ravi_ast.h>
static inline unsigned alloc_reg(struct pseudo_generator *generator) {
if (generator->free_pos > 0) {
return generator->free_regs[--generator->free_pos];
}
return generator->next_reg++;
}
static inline void free_reg(struct pseudo_generator *generator, unsigned reg) {
if (generator->free_pos == (sizeof generator->free_regs / sizeof generator->free_regs[0])) {
/* TODO proper error handling */
fprintf(stderr, "Out of register space\n");
abort();
}
generator->free_regs[generator->free_pos++] = (uint8_t) reg;
}
/* Linearizer - WIP */
static void ravi_init_linearizer(struct linearizer *linearizer, struct ast_container *container) {
@ -22,17 +40,45 @@ static void ravi_init_linearizer(struct linearizer *linearizer, struct ast_conta
dmrC_allocator_init(&linearizer->basic_block_allocator, "basic_block_allocator", sizeof(struct basic_block),
sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->proc_allocator, "proc_allocator", sizeof(struct proc), sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->unsized_allocator, "unsized_allocator", 0, sizeof(double), CHUNK);
dmrC_allocator_init(&linearizer->constant_allocator, "constant_allocator", sizeof(struct constant), sizeof(double), CHUNK);
}
static void ravi_destroy_linearizer(struct linearizer *linearizer) {
struct proc *proc;
FOR_EACH_PTR(linearizer->all_procs, proc) {
if (proc->constants)
set_destroy(proc->constants, NULL);
} END_FOR_EACH_PTR(proc);
dmrC_allocator_destroy(&linearizer->edge_allocator);
dmrC_allocator_destroy(&linearizer->instruction_allocator);
dmrC_allocator_destroy(&linearizer->ptrlist_allocator);
dmrC_allocator_destroy(&linearizer->pseudo_allocator);
dmrC_allocator_destroy(&linearizer->basic_block_allocator);
dmrC_allocator_destroy(&linearizer->proc_allocator);
dmrC_allocator_destroy(&linearizer->unsized_allocator);
dmrC_allocator_destroy(&linearizer->constant_allocator);
}
static int compare_constants(const void *a, const void *b) {
const struct constant *c1 = (const struct constant *)a;
const struct constant *c2 = (const struct constant *)b;
if (c1->type != c2->type)
return 1;
if (c1->type == RAVI_TNUMINT)
return c1->i - c2->i;
else if (c1->type == RAVI_TNUMFLT)
return c1->n == c2->n ? 0 : (c1->n < c2->n ? -1 : 1);
else
return c1->s == c2->s;
}
static uint32_t hash_constant(const void *c) {
return fnv1_hash_data(c, sizeof(struct constant));
}
/**
* Allocate a new proc. If there is a current proc, then the new proc gets added to the
* current procs children.
@ -41,11 +87,12 @@ static struct proc *allocate_proc(struct linearizer *linearizer, struct ast_node
assert(function_expr->type == AST_FUNCTION_EXPR);
struct proc *proc = dmrC_allocator_allocate(&linearizer->proc_allocator, 0);
proc->function_expr = function_expr;
ptrlist_add(&linearizer->all_procs, proc, &linearizer->ptrlist_allocator);
ptrlist_add((struct ptr_list **)&linearizer->all_procs, proc, &linearizer->ptrlist_allocator);
if (linearizer->current_proc) {
proc->parent = linearizer->current_proc;
ptrlist_add(&linearizer->current_proc, proc, &linearizer->ptrlist_allocator);
ptrlist_add((struct ptr_list **)&linearizer->current_proc->procs, proc, &linearizer->ptrlist_allocator);
}
proc->constants = set_create(hash_constant, compare_constants);
return proc;
}
@ -60,9 +107,198 @@ static inline void set_current_proc(struct linearizer *linearizer, struct proc *
linearizer->current_proc = proc;
}
static void linearize_function_args(struct linearizer *linearizer) {
struct proc *proc = linearizer->current_proc;
struct ast_node *func_expr = proc->function_expr;
struct lua_symbol *sym;
FOR_EACH_PTR(func_expr->function_expr.args, sym) {
uint8_t reg = alloc_reg(&proc->local_pseudos);
printf("Assigning register %d to argument %s\n", (int)reg, getstr(sym->var.var_name));
}
END_FOR_EACH_PTR(sym);
}
static void linearize_statement(struct linearizer *linearizer, struct ast_node *node);
static void linearize_statement_list(struct linearizer *linearizer, struct ast_node_list *list) {
struct ast_node *node;
FOR_EACH_PTR(list, node) {
linearize_statement(linearizer, node); }
END_FOR_EACH_PTR(node);
}
static int allocate_constant(struct linearizer *linearizer, struct ast_node *node) {
assert(node->type == AST_LITERAL_EXPR);
struct proc *proc = linearizer->current_proc;
struct constant c;
c.type = node->literal_expr.type.type_code;
if (c.type == RAVI_TNUMINT)
c.i = node->literal_expr.u.i;
else if (c.type == RAVI_TNUMFLT)
c.n = node->literal_expr.u.n;
else
c.s = node->literal_expr.u.s;
struct set_entry* entry = set_search(proc->constants, &c);
if (entry == NULL) {
int reg = proc->num_constants++;
struct constant *c1 = dmrC_allocator_allocate(&linearizer->constant_allocator, 0);
assert(c1);
memcpy(c1, &c, sizeof *c1);
c1->index = reg;
set_add(proc->constants, c1);
return reg;
}
else {
const struct constant *c1 = entry->key;
return c1->index;
}
}
static void linearize_statement(struct linearizer *linearizer, struct ast_node *node) {
switch (node->type) {
case AST_FUNCTION_EXPR: {
/* args need type assertions but those have no ast - i.e. code gen should do it */
//typecheck_ast_list(container, function, node->function_expr.function_statement_list);
break;
}
case AST_NONE: {
break;
}
case AST_RETURN_STMT: {
//typecheck_ast_list(container, function, node->return_stmt.expr_list);
break;
}
case AST_LOCAL_STMT: {
//typecheck_local_statement(container, function, node);
break;
}
case AST_FUNCTION_STMT: {
//typecheck_ast_node(container, function, node->function_stmt.function_expr);
break;
}
case AST_LABEL_STMT: {
break;
}
case AST_GOTO_STMT: {
break;
}
case AST_DO_STMT: {
break;
}
case AST_EXPR_STMT: {
//typecheck_expr_statement(container, function, node);
break;
}
case AST_IF_STMT: {
//typecheck_if_statement(container, function, node);
break;
}
case AST_WHILE_STMT:
case AST_REPEAT_STMT: {
//typecheck_while_or_repeat_statement(container, function, node);
break;
}
case AST_FORIN_STMT: {
//typecheck_for_in_statment(container, function, node);
break;
}
case AST_FORNUM_STMT: {
//typecheck_for_num_statment(container, function, node);
break;
}
case AST_SUFFIXED_EXPR: {
//typecheck_suffixedexpr(container, function, node);
break;
}
case AST_FUNCTION_CALL_EXPR: {
//if (node->function_call_expr.method_name) {
//}
//else {
//}
//typecheck_ast_list(container, function, node->function_call_expr.arg_list);
break;
}
case AST_SYMBOL_EXPR: {
/* symbol type should have been set when symbol was created */
//copy_type(node->symbol_expr.type, node->symbol_expr.var->value_type);
break;
}
case AST_BINARY_EXPR: {
//typecheck_binaryop(container, function, node);
break;
}
case AST_UNARY_EXPR: {
//typecheck_unaryop(container, function, node);
break;
}
case AST_LITERAL_EXPR: {
/* type set during parsing */
break;
}
case AST_FIELD_SELECTOR_EXPR: {
//typecheck_ast_node(container, function, node->index_expr.expr);
break;
}
case AST_Y_INDEX_EXPR: {
//typecheck_ast_node(container, function, node->index_expr.expr);
break;
}
case AST_INDEXED_ASSIGN_EXPR: {
//if (node->indexed_assign_expr.index_expr) {
// typecheck_ast_node(container, function, node->indexed_assign_expr.index_expr);
//}
//typecheck_ast_node(container, function, node->indexed_assign_expr.value_expr);
//copy_type(node->indexed_assign_expr.type, node->indexed_assign_expr.value_expr->common_expr.type);
break;
}
case AST_TABLE_EXPR: {
//typecheck_ast_list(container, function, node->table_expr.expr_list);
break;
}
default:
assert(0);
}
}
static struct basic_block *allocate_basic_block(struct linearizer *linearizer) {
struct proc *proc = linearizer->current_proc;
if (proc->node_count >= proc->allocated) {
unsigned new_size = proc->allocated + 25;
struct node ** new_data = dmrC_allocator_allocate(&linearizer->unsized_allocator, new_size * sizeof(struct node *));
assert(new_data != NULL);
if (proc->node_count > 0) {
memcpy(new_data, proc->nodes, proc->allocated*sizeof(struct node *));
}
proc->allocated = new_size;
}
assert(proc->node_count < proc->allocated);
struct basic_block *new_block = dmrC_allocator_allocate(&linearizer->basic_block_allocator, 0);
proc->nodes[proc->node_count++] = bb2n(new_block);
return new_block;
}
static void init_blocks(struct linearizer *linearizer) {
struct proc *proc = linearizer->current_proc;
assert(proc != NULL);
proc->entry = bb2n(allocate_basic_block(linearizer));
proc->exit = bb2n(allocate_basic_block(linearizer));
}
static void linearize_function(struct linearizer *linearizer) {
struct proc *proc = linearizer->current_proc;
assert(proc != NULL);
struct ast_node *func_expr = proc->function_expr;
assert(func_expr->type == AST_FUNCTION_EXPR);
init_blocks(linearizer);
proc->current_scope = func_expr->function_expr.main_block;
linearize_function_args(linearizer);
linearize_statement_list(linearizer, func_expr->function_expr.function_statement_list);
}
void raviA_ast_linearize(struct linearizer *linearizer, struct ast_container *container) {
ravi_init_linearizer(linearizer, container);
struct proc *proc = allocate_proc(linearizer, container->main_function);
set_main_proc(linearizer, proc);
set_current_proc(linearizer, proc);
linearize_function(linearizer);
ravi_destroy_linearizer(linearizer);
}

@ -118,6 +118,7 @@ static struct lua_symbol *new_local_symbol(struct parser_state *parser, TString
symbol->symbol_type = SYM_LOCAL;
symbol->var.block = scope;
symbol->var.var_name = name;
symbol->var.backend_data = NULL;
add_symbol(parser->container, &scope->symbol_list, symbol); // Add to the end of the symbol list
add_symbol(parser->container, &scope->function->function_expr.locals, symbol);
// Note that Lua allows multiple local declarations of the same name

@ -361,6 +361,7 @@ static void typecheck_while_or_repeat_statement(struct ast_container *container,
static void typecheck_ast_node(struct ast_container *container, struct ast_node *function, struct ast_node *node) {
switch (node->type) {
case AST_FUNCTION_EXPR: {
/* args need type assertions but those have no ast - i.e. code gen should do it */
typecheck_ast_list(container, function, node->function_expr.function_statement_list);
break;
}

@ -0,0 +1,72 @@
/*
* Copyright © 2009 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Authors:
* Eric Anholt <eric@anholt.net>
*/
/* Quick FNV-1a hash implementation based on:
* http://www.isthe.com/chongo/tech/comp/fnv/
*
* FNV-1a may not be the best hash out there -- Jenkins's lookup3 is supposed
* to be quite good, and it may beat FNV. But FNV has the advantage that it
* involves almost no code.
*/
#include <string.h>
#include <ravi_fnv_hash.h>
uint32_t
fnv1_hash_string(const char *key)
{
uint32_t hash = 2166136261ul;
const uint8_t *bytes = (uint8_t *)key;
while (*bytes != 0) {
hash ^= *bytes;
hash = hash * 0x01000193;
bytes++;
}
return hash;
}
uint32_t
fnv1_hash_data(const void *data, size_t size)
{
uint32_t hash = 2166136261ul;
const uint8_t *bytes = (uint8_t *)data;
while (size-- != 0) {
hash ^= *bytes;
hash = hash * 0x01000193;
bytes++;
}
return hash;
}
int
string_key_equals(const void *a, const void *b)
{
return strcmp(a, b) == 0;
}

@ -0,0 +1,423 @@
/*
* Copyright © 2009 Intel Corporation
* Copyright © 1988-2004 Keith Packard and Bart Massey.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors
* or their institutions shall not be used in advertising or
* otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from the
* authors.
*
* Authors:
* Eric Anholt <eric@anholt.net>
* Keith Packard <keithp@keithp.com>
*/
#include <assert.h>
#include <stdlib.h>
#include <ravi_hash_table.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
/*
* From Knuth -- a good choice for hash/rehash values is p, p-2 where
* p and p-2 are both prime. These tables are sized to have an extra 10%
* free to avoid exponential performance degradation as the hash table fills
*/
static const uint32_t deleted_key_value;
static const void *deleted_key = &deleted_key_value;
static const struct {
uint32_t max_entries, size, rehash;
} hash_sizes[] = {
{ 2, 5, 3 },
{ 4, 7, 5 },
{ 8, 13, 11 },
{ 16, 19, 17 },
{ 32, 43, 41 },
{ 64, 73, 71 },
{ 128, 151, 149 },
{ 256, 283, 281 },
{ 512, 571, 569 },
{ 1024, 1153, 1151 },
{ 2048, 2269, 2267 },
{ 4096, 4519, 4517 },
{ 8192, 9013, 9011 },
{ 16384, 18043, 18041 },
{ 32768, 36109, 36107 },
{ 65536, 72091, 72089 },
{ 131072, 144409, 144407 },
{ 262144, 288361, 288359 },
{ 524288, 576883, 576881 },
{ 1048576, 1153459, 1153457 },
{ 2097152, 2307163, 2307161 },
{ 4194304, 4613893, 4613891 },
{ 8388608, 9227641, 9227639 },
{ 16777216, 18455029, 18455027 },
{ 33554432, 36911011, 36911009 },
{ 67108864, 73819861, 73819859 },
{ 134217728, 147639589, 147639587 },
{ 268435456, 295279081, 295279079 },
{ 536870912, 590559793, 590559791 },
{ 1073741824, 1181116273, 1181116271},
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
static int
entry_is_free(const struct hash_entry *entry)
{
return entry->key == NULL;
}
static int
entry_is_deleted(const struct hash_entry *entry)
{
return entry->key == deleted_key;
}
static int
entry_is_present(const struct hash_entry *entry)
{
return entry->key != NULL && entry->key != deleted_key;
}
struct hash_table *
hash_table_create(uint32_t (*hash_function)(const void *key),
int (*key_equals_function)(const void *a,
const void *b))
{
struct hash_table *ht;
ht = malloc(sizeof(*ht));
if (ht == NULL)
return NULL;
ht->size_index = 0;
ht->size = hash_sizes[ht->size_index].size;
ht->rehash = hash_sizes[ht->size_index].rehash;
ht->max_entries = hash_sizes[ht->size_index].max_entries;
ht->hash_function = hash_function;
ht->key_equals_function = key_equals_function;
ht->table = calloc(ht->size, sizeof(*ht->table));
ht->entries = 0;
ht->deleted_entries = 0;
if (ht->table == NULL) {
free(ht);
return NULL;
}
return ht;
}
/**
* Frees the given hash table.
*
* If delete_function is passed, it gets called on each entry present before
* freeing.
*/
void
hash_table_destroy(struct hash_table *ht,
void (*delete_function)(struct hash_entry *entry))
{
if (!ht)
return;
if (delete_function) {
struct hash_entry *entry;
hash_table_foreach(ht, entry) {
delete_function(entry);
}
}
free(ht->table);
free(ht);
}
/**
* Finds a hash table entry with the given key.
*
* Returns NULL if no entry is found. Note that the data pointer may be
* modified by the user.
*/
struct hash_entry *
hash_table_search(struct hash_table *ht, const void *key)
{
uint32_t hash = ht->hash_function(key);
return hash_table_search_pre_hashed(ht, hash, key);
}
/**
* Finds a hash table entry with the given key and hash of that key.
*
* Returns NULL if no entry is found. Note that the data pointer may be
* modified by the user.
*/
struct hash_entry *
hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash,
const void *key)
{
uint32_t start_hash_address = hash % ht->size;
uint32_t hash_address = start_hash_address;
do {
uint32_t double_hash;
struct hash_entry *entry = ht->table + hash_address;
if (entry_is_free(entry)) {
return NULL;
} else if (entry_is_present(entry) && entry->hash == hash) {
if (ht->key_equals_function(key, entry->key)) {
return entry;
}
}
double_hash = 1 + hash % ht->rehash;
hash_address = (hash_address + double_hash) % ht->size;
} while (hash_address != start_hash_address);
return NULL;
}
static void
hash_table_rehash(struct hash_table *ht, int new_size_index)
{
struct hash_table old_ht;
struct hash_entry *table, *entry;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
table = calloc(hash_sizes[new_size_index].size, sizeof(*ht->table));
if (table == NULL)
return;
old_ht = *ht;
ht->table = table;
ht->size_index = new_size_index;
ht->size = hash_sizes[ht->size_index].size;
ht->rehash = hash_sizes[ht->size_index].rehash;
ht->max_entries = hash_sizes[ht->size_index].max_entries;
ht->entries = 0;
ht->deleted_entries = 0;
hash_table_foreach(&old_ht, entry) {
hash_table_insert_pre_hashed(ht, entry->hash,
entry->key, entry->data);
}
free(old_ht.table);
}
/**
* Inserts the key into the table.
*
* Note that insertion may rearrange the table on a resize or rehash,
* so previously found hash_entries are no longer valid after this function.
*/
struct hash_entry *
hash_table_insert(struct hash_table *ht, const void *key, void *data)
{
uint32_t hash = ht->hash_function(key);
/* Make sure nobody tries to add one of the magic values as a
* key. If you need to do so, either do so in a wrapper, or
* store keys with the magic values separately in the struct
* hash_table.
*/
assert(key != NULL);
return hash_table_insert_pre_hashed(ht, hash, key, data);
}
/**
* Inserts the key with the given hash into the table.
*
* Note that insertion may rearrange the table on a resize or rehash,
* so previously found hash_entries are no longer valid after this function.
*/
struct hash_entry *
hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash,
const void *key, void *data)
{
uint32_t start_hash_address, hash_address;
struct hash_entry *available_entry = NULL;
if (ht->entries >= ht->max_entries) {
hash_table_rehash(ht, ht->size_index + 1);
} else if (ht->deleted_entries + ht->entries >= ht->max_entries) {
hash_table_rehash(ht, ht->size_index);
}
start_hash_address = hash % ht->size;
hash_address = start_hash_address;
do {
struct hash_entry *entry = ht->table + hash_address;
uint32_t double_hash;
if (!entry_is_present(entry)) {
/* Stash the first available entry we find */
if (available_entry == NULL)
available_entry = entry;
if (entry_is_free(entry))
break;
}
/* Implement replacement when another insert happens
* with a matching key. This is a relatively common
* feature of hash tables, with the alternative
* generally being "insert the new value as well, and
* return it first when the key is searched for".
*
* Note that the hash table doesn't have a delete
* callback. If freeing of old data pointers is
* required to avoid memory leaks, perform a search
* before inserting.
*/
if (!entry_is_deleted(entry) &&
entry->hash == hash &&
ht->key_equals_function(key, entry->key)) {
entry->key = key;
entry->data = data;
return entry;
}
double_hash = 1 + hash % ht->rehash;
hash_address = (hash_address + double_hash) % ht->size;
} while (hash_address != start_hash_address);
if (available_entry) {
if (entry_is_deleted(available_entry))
ht->deleted_entries--;
available_entry->hash = hash;
available_entry->key = key;
available_entry->data = data;
ht->entries++;
return available_entry;
}
/* We could hit here if a required resize failed. An unchecked-malloc
* application could ignore this result.
*/
return NULL;
}
/**
* This function searches for, and removes an entry from the hash table.
*
* If the caller has previously found a struct hash_entry pointer,
* (from calling hash_table_search or remembering it from
* hash_table_insert), then hash_table_remove_entry can be called
* instead to avoid an extra search.
*/
void
hash_table_remove(struct hash_table *ht, const void *key)
{
struct hash_entry *entry;
entry = hash_table_search(ht, key);
hash_table_remove_entry(ht, entry);
}
/**
* This function deletes the given hash table entry.
*
* Note that deletion doesn't otherwise modify the table, so an iteration over
* the table deleting entries is safe.
*/
void
hash_table_remove_entry(struct hash_table *ht, struct hash_entry *entry)
{
if (!entry)
return;
entry->key = deleted_key;
ht->entries--;
ht->deleted_entries++;
}
/**
* This function is an iterator over the hash table.
*
* Pass in NULL for the first entry, as in the start of a for loop. Note that
* an iteration over the table is O(table_size) not O(entries).
*/
struct hash_entry *
hash_table_next_entry(struct hash_table *ht, struct hash_entry *entry)
{
if (entry == NULL)
entry = ht->table;
else
entry = entry + 1;
for (; entry != ht->table + ht->size; entry++) {
if (entry_is_present(entry)) {
return entry;
}
}
return NULL;
}
/**
* Returns a random entry from the hash table.
*
* This may be useful in implementing random replacement (as opposed
* to just removing everything) in caches based on this hash table
* implementation. @predicate may be used to filter entries, or may
* be set to NULL for no filtering.
*/
struct hash_entry *
hash_table_random_entry(struct hash_table *ht,
int (*predicate)(struct hash_entry *entry))
{
struct hash_entry *entry;
uint32_t i = random() % ht->size;
if (ht->entries == 0)
return NULL;
for (entry = ht->table + i; entry != ht->table + ht->size; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
for (entry = ht->table; entry != ht->table + i; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
return NULL;
}

@ -0,0 +1,356 @@
/*
* Copyright © 2009,2013 Intel Corporation
* Copyright © 1988-2004 Keith Packard and Bart Massey.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors
* or their institutions shall not be used in advertising or
* otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from the
* authors.
*
* Authors:
* Eric Anholt <eric@anholt.net>
* Keith Packard <keithp@keithp.com>
* Carl Worth <cworth@cworth.org>
*/
#include <stdlib.h>
#include <ravi_int_set.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
/*
* From Knuth -- a good choice for hash/rehash values is p, p-2 where
* p and p-2 are both prime. These tables are sized to have an extra 10%
* free to avoid exponential performance degradation as the hash table fills
*/
static const struct {
uint32_t max_entries, size, rehash;
} hash_sizes[] = {
{ 2, 5, 3 },
{ 4, 7, 5 },
{ 8, 13, 11 },
{ 16, 19, 17 },
{ 32, 43, 41 },
{ 64, 73, 71 },
{ 128, 151, 149 },
{ 256, 283, 281 },
{ 512, 571, 569 },
{ 1024, 1153, 1151 },
{ 2048, 2269, 2267 },
{ 4096, 4519, 4517 },
{ 8192, 9013, 9011 },
{ 16384, 18043, 18041 },
{ 32768, 36109, 36107 },
{ 65536, 72091, 72089 },
{ 131072, 144409, 144407 },
{ 262144, 288361, 288359 },
{ 524288, 576883, 576881 },
{ 1048576, 1153459, 1153457 },
{ 2097152, 2307163, 2307161 },
{ 4194304, 4613893, 4613891 },
{ 8388608, 9227641, 9227639 },
{ 16777216, 18455029, 18455027 },
{ 33554432, 36911011, 36911009 },
{ 67108864, 73819861, 73819859 },
{ 134217728, 147639589, 147639587 },
{ 268435456, 295279081, 295279079 },
{ 536870912, 590559793, 590559791 },
{ 1073741824, 1181116273, 1181116271},
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
static int
entry_is_free(struct int_set_entry *entry)
{
return ! entry->occupied;
}
static int
entry_is_deleted(struct int_set_entry *entry)
{
return entry->deleted;
}
static int
entry_is_present(struct int_set_entry *entry)
{
return entry->occupied && ! entry->deleted;
}
struct int_set *
int_set_create(void)
{
struct int_set *set;
set = malloc(sizeof(*set));
if (set == NULL)
return NULL;
set->size_index = 0;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->table = calloc(set->size, sizeof(*set->table));
set->entries = 0;
set->deleted_entries = 0;
if (set->table == NULL) {
free(set);
return NULL;
}
return set;
}
/**
* Frees the given set.
*/
void
int_set_destroy(struct int_set *set)
{
if (!set)
return;
free(set->table);
free(set);
}
/* Does the set contain an entry with the given value.
*/
bool
int_set_contains(struct int_set *set, uint32_t value)
{
struct int_set_entry *entry;
entry = int_set_search(set, value);
return entry != NULL;
}
/**
* Finds a set entry with the given value
*
* Returns NULL if no entry is found.
*/
struct int_set_entry *
int_set_search(struct int_set *set, uint32_t value)
{
uint32_t hash_address;
hash_address = value % set->size;
do {
uint32_t double_hash;
struct int_set_entry *entry = set->table + hash_address;
if (entry_is_free(entry)) {
return NULL;
} else if (entry_is_present(entry) && entry->value == value) {
return entry;
}
double_hash = 1 + value % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != value % set->size);
return NULL;
}
static void
int_set_rehash(struct int_set *set, int new_size_index)
{
struct int_set old_set;
struct int_set_entry *table, *entry;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
table = calloc(hash_sizes[new_size_index].size, sizeof(*set->table));
if (table == NULL)
return;
old_set = *set;
set->table = table;
set->size_index = new_size_index;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->entries = 0;
set->deleted_entries = 0;
for (entry = old_set.table;
entry != old_set.table + old_set.size;
entry++) {
if (entry_is_present(entry)) {
int_set_add(set, entry->value);
}
}
free(old_set.table);
}
/**
* Inserts the given value into the set.
*
* Note that insertion may rearrange the table on a resize or rehash,
* so previously found int_set_entry pointers are no longer valid
* after this function.
*/
struct int_set_entry *
int_set_add(struct int_set *set, uint32_t value)
{
uint32_t hash_address;
struct int_set_entry *available_entry = NULL;
if (set->entries >= set->max_entries) {
int_set_rehash(set, set->size_index + 1);
} else if (set->deleted_entries + set->entries >= set->max_entries) {
int_set_rehash(set, set->size_index);
}
hash_address = value % set->size;
do {
struct int_set_entry *entry = set->table + hash_address;
uint32_t double_hash;
if (!entry_is_present(entry)) {
if (available_entry == NULL)
available_entry = entry;
if (entry_is_free(entry))
break;
if (entry_is_deleted(entry)) {
set->deleted_entries--;
entry->deleted = 0;
}
}
if (entry->value == value) {
return entry;
}
double_hash = 1 + value % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != value % set->size);
if (available_entry) {
available_entry->value = value;
available_entry->occupied = 1;
set->entries++;
return available_entry;
}
/* We could hit here if a required resize failed. An unchecked-malloc
* application could ignore this result.
*/
return NULL;
}
/**
* This function searches for, and removes an entry from the set.
*
* If the caller has previously found a struct int_set_entry pointer,
* (from calling int_set_search or remembering it from int_set_add),
* then int_set_remove_entry can be called instead to avoid an extra
* search.
*/
void
int_set_remove(struct int_set *set, uint32_t value)
{
struct int_set_entry *entry;
entry = int_set_search(set, value);
int_set_remove_entry(set, entry);
}
/**
* This function deletes the given set entry.
*
* Note that deletion doesn't otherwise modify the table, so an iteration over
* the table deleting entries is safe.
*/
void
int_set_remove_entry(struct int_set *set, struct int_set_entry *entry)
{
if (!entry)
return;
entry->deleted = 1;
set->entries--;
set->deleted_entries++;
}
/**
* This function is an iterator over the set.
*
* Pass in NULL for the first entry, as in the start of a for loop. Note that
* an iteration over the table is O(table_size) not O(entries).
*/
struct int_set_entry *
int_set_next_entry(struct int_set *set, struct int_set_entry *entry)
{
if (entry == NULL)
entry = set->table;
else
entry = entry + 1;
for (; entry != set->table + set->size; entry++) {
if (entry_is_present(entry)) {
return entry;
}
}
return NULL;
}
struct int_set_entry *
int_set_random_entry(struct int_set *set,
int (*predicate)(struct int_set_entry *entry))
{
struct int_set_entry *entry;
uint32_t i = random() % set->size;
if (set->entries == 0)
return NULL;
for (entry = set->table + i; entry != set->table + set->size; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
for (entry = set->table; entry != set->table + i; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
return NULL;
}

@ -0,0 +1,418 @@
/*
* Copyright © 2009 Intel Corporation
* Copyright © 1988-2004 Keith Packard and Bart Massey.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* Except as contained in this notice, the names of the authors
* or their institutions shall not be used in advertising or
* otherwise to promote the sale, use or other dealings in this
* Software without prior written authorization from the
* authors.
*
* Authors:
* Eric Anholt <eric@anholt.net>
* Keith Packard <keithp@keithp.com>
*/
#include <assert.h>
#include <stdlib.h>
#include <ravi_set.h>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
/*
* From Knuth -- a good choice for hash/rehash values is p, p-2 where
* p and p-2 are both prime. These tables are sized to have an extra 10%
* free to avoid exponential performance degradation as the hash table fills
*/
static const uint32_t deleted_key_value;
static const void *deleted_key = &deleted_key_value;
static const struct {
uint32_t max_entries, size, rehash;
} hash_sizes[] = {
{ 2, 5, 3 },
{ 4, 7, 5 },
{ 8, 13, 11 },
{ 16, 19, 17 },
{ 32, 43, 41 },
{ 64, 73, 71 },
{ 128, 151, 149 },
{ 256, 283, 281 },
{ 512, 571, 569 },
{ 1024, 1153, 1151 },
{ 2048, 2269, 2267 },
{ 4096, 4519, 4517 },
{ 8192, 9013, 9011 },
{ 16384, 18043, 18041 },
{ 32768, 36109, 36107 },
{ 65536, 72091, 72089 },
{ 131072, 144409, 144407 },
{ 262144, 288361, 288359 },
{ 524288, 576883, 576881 },
{ 1048576, 1153459, 1153457 },
{ 2097152, 2307163, 2307161 },
{ 4194304, 4613893, 4613891 },
{ 8388608, 9227641, 9227639 },
{ 16777216, 18455029, 18455027 },
{ 33554432, 36911011, 36911009 },
{ 67108864, 73819861, 73819859 },
{ 134217728, 147639589, 147639587 },
{ 268435456, 295279081, 295279079 },
{ 536870912, 590559793, 590559791 },
{ 1073741824, 1181116273, 1181116271},
{ 2147483648ul, 2362232233ul, 2362232231ul}
};
static int
entry_is_free(const struct set_entry *entry)
{
return entry->key == NULL;
}
static int
entry_is_deleted(const struct set_entry *entry)
{
return entry->key == deleted_key;
}
static int
entry_is_present(const struct set_entry *entry)
{
return entry->key != NULL && entry->key != deleted_key;
}
struct set *
set_create(uint32_t (*hash_function)(const void *key),
int key_equals_function(const void *a,
const void *b))
{
struct set *set;
set = malloc(sizeof(*set));
if (set == NULL)
return NULL;
set->size_index = 0;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->hash_function = hash_function;
set->key_equals_function = key_equals_function;
set->table = calloc(set->size, sizeof(*set->table));
set->entries = 0;
set->deleted_entries = 0;
if (set->table == NULL) {
free(set);
return NULL;
}
return set;
}
/**
* Frees the given set.
*
* If delete_function is passed, it gets called on each entry present before
* freeing.
*/
void
set_destroy(struct set *set, void (*delete_function)(struct set_entry *entry))
{
if (!set)
return;
if (delete_function) {
struct set_entry *entry;
set_foreach(set, entry) {
delete_function(entry);
}
}
free(set->table);
free(set);
}
/* Does the set contain an entry with the given key.
*/
bool
set_contains(struct set *set, const void *key)
{
struct set_entry *entry;
entry = set_search(set, key);
return entry != NULL;
}
/**
* Finds a set entry with the given key.
*
* Returns NULL if no entry is found.
*/
struct set_entry *
set_search(struct set *set, const void *key)
{
uint32_t hash = set->hash_function(key);
return set_search_pre_hashed (set, hash, key);
}
/**
* Finds a set entry with the given key and hash of that key.
*
* Returns NULL if no entry is found.
*/
struct set_entry *
set_search_pre_hashed(struct set *set, uint32_t hash, const void *key)
{
uint32_t hash_address;
hash_address = hash % set->size;
do {
uint32_t double_hash;
struct set_entry *entry = set->table + hash_address;
if (entry_is_free(entry)) {
return NULL;
} else if (entry_is_present(entry) && entry->hash == hash) {
if (set->key_equals_function(key, entry->key)) {
return entry;
}
}
double_hash = 1 + hash % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != hash % set->size);
return NULL;
}
static void
set_rehash(struct set *set, int new_size_index)
{
struct set old_set;
struct set_entry *table, *entry;
if (new_size_index >= ARRAY_SIZE(hash_sizes))
return;
table = calloc(hash_sizes[new_size_index].size, sizeof(*set->table));
if (table == NULL)
return;
old_set = *set;
set->table = table;
set->size_index = new_size_index;
set->size = hash_sizes[set->size_index].size;
set->rehash = hash_sizes[set->size_index].rehash;
set->max_entries = hash_sizes[set->size_index].max_entries;
set->entries = 0;
set->deleted_entries = 0;
set_foreach(&old_set, entry) {
set_add_pre_hashed(set, entry->hash, entry->key);
}
free(old_set.table);
}
/**
* Inserts the key into the set.
*
* Note that insertion may rearrange the set on a resize or rehash, so
* previously found set_entry pointers are no longer valid after this
* function.
*/
struct set_entry *
set_add(struct set *set, const void *key)
{
uint32_t hash = set->hash_function(key);
/* Make sure nobody tries to add one of the magic values as a
* key. If you need to do so, either do so in a wrapper, or
* store keys with the magic values separately in the struct
* set.
*/
assert(key != NULL);
return set_add_pre_hashed(set, hash, key);
}
/**
* Inserts the key with the given hash into the set.
*
* Note that insertion may rearrange the set on a resize or rehash, so
* previously found set_entry pointers are no longer valid after this
* function.
*/
struct set_entry *
set_add_pre_hashed(struct set *set, uint32_t hash, const void *key)
{
uint32_t hash_address;
struct set_entry *available_entry = NULL;
if (set->entries >= set->max_entries) {
set_rehash(set, set->size_index + 1);
} else if (set->deleted_entries + set->entries >= set->max_entries) {
set_rehash(set, set->size_index);
}
hash_address = hash % set->size;
do {
struct set_entry *entry = set->table + hash_address;
uint32_t double_hash;
if (!entry_is_present(entry)) {
/* Stash the first available entry we find */
if (available_entry == NULL)
available_entry = entry;
if (entry_is_free(entry))
break;
}
/* Implement replacement when another insert happens
* with a matching key. This is a relatively common
* feature of hash tables, with the alternative
* generally being "insert the new value as well, and
* return it first when the key is searched for".
*
* Note that the set doesn't have a delete callback.
* If freeing of old keys is required to avoid memory leaks,
* perform a search before inserting.
*/
if (!entry_is_deleted(entry) &&
entry->hash == hash &&
set->key_equals_function(key, entry->key)) {
entry->key = key;
return entry;
}
double_hash = 1 + hash % set->rehash;
hash_address = (hash_address + double_hash) % set->size;
} while (hash_address != hash % set->size);
if (available_entry) {
if (entry_is_deleted(available_entry))
set->deleted_entries--;
available_entry->hash = hash;
available_entry->key = key;
set->entries++;
return available_entry;
}
/* We could hit here if a required resize failed. An unchecked-malloc
* application could ignore this result.
*/
return NULL;
}
/**
* This function searches for, and removes an entry from the set.
*
* If the caller has previously found a struct set_entry pointer,
* (from calling set_search or remembering it from set_add), then
* set_remove_entry can be called instead to avoid an extra search.
*/
void
set_remove(struct set *set, const void *key)
{
struct set_entry *entry;
entry = set_search(set, key);
set_remove_entry(set, entry);
}
/**
* This function deletes the set given set entry.
*
* Note that deletion doesn't otherwise modify the set, so an
* iteration over the set deleting entries is safe.
*/
void
set_remove_entry(struct set *set, struct set_entry *entry)
{
if (!entry)
return;
entry->key = deleted_key;
set->entries--;
set->deleted_entries++;
}
/**
* This function is an iterator over the set.
*
* Pass in NULL for the first entry, as in the start of a for loop.
* Note that an iteration over the set is O(table_size) not
* O(entries).
*/
struct set_entry *
set_next_entry(struct set *set, struct set_entry *entry)
{
if (entry == NULL)
entry = set->table;
else
entry = entry + 1;
for (; entry != set->table + set->size; entry++) {
if (entry_is_present(entry)) {
return entry;
}
}
return NULL;
}
struct set_entry *
set_random_entry(struct set *set,
int (*predicate)(struct set_entry *entry))
{
struct set_entry *entry;
uint32_t i = random() % set->size;
if (set->entries == 0)
return NULL;
for (entry = set->table + i; entry != set->table + set->size; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
for (entry = set->table; entry != set->table + i; entry++) {
if (entry_is_present(entry) &&
(!predicate || predicate(entry))) {
return entry;
}
}
return NULL;
}
Loading…
Cancel
Save