Skip to content

Commit

Permalink
Mulentry (#29)
Browse files Browse the repository at this point in the history
* Multiple entries in main function.

Co-authored-by: smaludzi <[email protected]>
  • Loading branch information
smaludzi and smaludzi authored Jun 6, 2020
1 parent 86e217a commit c658901
Show file tree
Hide file tree
Showing 32 changed files with 4,206 additions and 3,461 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ test_dlcache

# build
build/
build_js/
build_wasm/
front/parser.h
front/parser.c
front/parser.output
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,50 @@ Never can be installed in steps:
* ```mkdir build; cd build```
* ```cmake ..; make```

### asm.js wasm

To compile for asm.js or wasm targets use the following commands:

Get the latest sdk: git clone https://github.com/emscripten-core/emsdk.git

```bash
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
```

Compile to native asm.js

```bash
mkdir build && cd build
emcmake cmake .. -DJS_ONLY=ON
make
```

Compile to native wasm

```bash
mkdir build && cd build
emcmake cmake .. -DWASM_ONLY=ON
make
```

Run

Now you should see never.js file in your build directory. You can overwrite ../never.js and open never.html, or modify never.html to load build/never.js file or load the file in JS console and run:

```bash
python -m SimpleHTTPServer 8000
# http://localhost:8000/never.html
```

```js
var never = Module.cwrap('never','number',['string']);
never("func main() -> int { 123 }")
```


## Contributing
All help is welcome! Using it, reporting bugs, spreading the word, writing
code samples, blogs, submitting ideas, documentation, new features. Everyone
Expand Down
6 changes: 6 additions & 0 deletions back/bytecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bytecode_op_str bytecode_op[] = {
{ BYTECODE_ID_GLOBAL, bytecode_print_id_global },
{ BYTECODE_ID_FUNC_FUNC, bytecode_print_id_func_func },
{ BYTECODE_ID_FUNC_ADDR, bytecode_print_id_func_addr },
{ BYTECODE_ID_FUNC_ENTRY, bytecode_print_id_func_entry },

{ BYTECODE_OP_NEG_INT, bytecode_print_op_neg_int },
{ BYTECODE_OP_ADD_INT, bytecode_print_op_add_int },
Expand Down Expand Up @@ -286,6 +287,11 @@ void bytecode_print_id_func_addr(bytecode * code)
printf("%d: id func addr %d\n", code->addr, code->id_func.func_addr);
}

void bytecode_print_id_func_entry(bytecode * code)
{
printf("%d: id func entry\n", code->addr);
}

void bytecode_print_op_neg_int(bytecode * code)
{
printf("%d: op neg int\n", code->addr);
Expand Down
2 changes: 2 additions & 0 deletions back/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ typedef enum bytecode_type
BYTECODE_ID_GLOBAL,
BYTECODE_ID_FUNC_FUNC,
BYTECODE_ID_FUNC_ADDR,
BYTECODE_ID_FUNC_ENTRY,

BYTECODE_OP_NEG_INT,
BYTECODE_OP_ADD_INT,
Expand Down Expand Up @@ -375,6 +376,7 @@ void bytecode_print_id_dim_slice(bytecode * code);
void bytecode_print_id_global(bytecode * code);
void bytecode_print_id_func_func(bytecode * code);
void bytecode_print_id_func_addr(bytecode * code);
void bytecode_print_id_func_entry(bytecode * code);

void bytecode_print_op_neg_int(bytecode * code);
void bytecode_print_op_add_int(bytecode * code);
Expand Down
184 changes: 184 additions & 0 deletions back/functab.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* Copyright 2020 Slawomir Maludzinski
*
* 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 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.
*/
#include "functab.h"
#include "func.h"
#include "object.h"
#include "hash.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>

functab_entry * functab_entry_new(unsigned int size)
{
functab_entry * entries = (functab_entry *)malloc(size * sizeof(functab_entry));
memset(entries, 0, size * sizeof(functab_entry));

return entries;
}

void functab_entry_delete(functab_entry * entries, unsigned int size)
{
unsigned int i = 0;

for (i = 0; i < size; i++)
{
if (entries[i].type == FUNCTAB_ADDR && entries[i].id != NULL)
{
free(entries[i].id);
}
if (entries[i].params != NULL)
{
object_delete(entries[i].params);
}
}

free(entries);
}

void functab_entry_add_func(functab_entry * entries, unsigned int size,
func * func_value, object * params, unsigned int params_count)
{
unsigned int times = 0;
unsigned int index = 0;

index = hash_string(func_value->decl->id) % size;
while (entries[index].id != 0)
{
index = (index + 1) % size;
if (times++ > size)
{
assert(0);
return;
}
}

entries[index].id = func_value->decl->id;
entries[index].func_addr = 0;
entries[index].func_value = func_value;
entries[index].params = params;
entries[index].params_count = params_count;
}

functab_entry * functab_entry_lookup(functab_entry * entries, unsigned int size,
const char * id)
{
unsigned int times = 0;
unsigned int index = 0;

index = hash_string(id) % size;
while (entries[index].id != 0)
{
if (strcmp(entries[index].id, id) == 0)
{
return &entries[index];
}

index = (index + 1) % size;
if (times++ > size)
{
return NULL;
}
}

return NULL;
}

void functab_entry_resize(functab_entry * entries, unsigned int size,
functab_entry * entries_new, unsigned int size_new)
{
unsigned int i = 0;

for (i = 0; i < size; i++)
{
if (entries[i].id != NULL)
{
functab_entry_add_func(entries_new, size_new,
entries[i].func_value,
entries[i].params,
entries[i].params_count);
}
}
}

functab * functab_new(unsigned int size)
{
functab * tab = (functab *)malloc(sizeof(functab));

tab->size = size;
tab->count = 0;
tab->entries = functab_entry_new(size);

return tab;
}

void functab_delete(functab * tab)
{
if (tab->entries)
{
functab_entry_delete(tab->entries, tab->size);
}
free(tab);
}

void functab_resize(functab * tab)
{
if (tab->count > tab->size * 3 / 4)
{
unsigned int size_new = 2 * tab->size;
functab_entry * entries_new = functab_entry_new(size_new);

functab_entry_resize(tab->entries, tab->size, entries_new, size_new);
free(tab->entries);
tab->size = size_new;
tab->entries = entries_new;
}
}

void functab_close(functab * tab)
{
unsigned int i = 0;

for (i = 0 ; i < tab->size; i++)
{
if (tab->entries[i].func_value != NULL)
{
tab->entries[i].type = FUNCTAB_ADDR;
tab->entries[i].id = strdup(tab->entries[i].func_value->decl->id);
tab->entries[i].func_addr = tab->entries[i].func_value->addr;
}
}
}

void functab_add_func(functab * tab, func * func_value,
object * params, unsigned int params_count)
{
functab_entry_add_func(tab->entries, tab->size, func_value, params, params_count);

tab->count++;
functab_resize(tab);
}

functab_entry * functab_lookup(functab * tab, const char * id)
{
return functab_entry_lookup(tab->entries, tab->size, id);
}
69 changes: 69 additions & 0 deletions back/functab.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2020 Slawomir Maludzinski
*
* 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 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.
*/
#ifndef __FUNCTAB_H__
#define __FUNCTAB_H__

typedef struct func func;
typedef struct object object;

typedef enum functab_type
{
FUNCTAB_FUNC = 0,
FUNCTAB_ADDR = 1
} functab_type;

typedef struct functab_entry
{
functab_type type;
func * func_value;
char * id;
unsigned int func_addr;
object * params;
unsigned int params_count;
} functab_entry;

typedef struct functab
{
unsigned int size;
unsigned int count;
struct functab_entry * entries;
} functab;

functab_entry * functab_entry_new(unsigned int size);
void functab_entry_delete(functab_entry * entries, unsigned int size);

void functab_entry_add_func(functab_entry * entries, unsigned int size,
func * func_value, object * params, unsigned int params_count);
functab_entry * functab_entry_lookup(functab_entry * entries, unsigned int size,
const char * id);
void functab_entry_resize(functab_entry * entries, unsigned int size,
functab_entry * entries_new, unsigned int size_new);

functab * functab_new(unsigned int size);
void functab_delete(functab * tab);

void functab_resize(functab * tab);
void functab_close(functab * tab);
void functab_add_func(functab * tab, func * func_value, object * params, unsigned int params_count);
functab_entry * functab_lookup(functab * tab, const char * id);

#endif /* __FUNCTAB_H__ */
12 changes: 12 additions & 0 deletions back/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ module * module_new()
value->strtab_array = NULL;
value->strtab_size = 0;

value->functab_value = functab_new(8);

value->code_arr = NULL;
value->code_size = 0;
value->code = bytecode_new();

value->code_entry = 0;

value->exctab_value = exception_tab_new(32);

return value;
Expand All @@ -49,6 +53,10 @@ void module_delete(module * value)
{
strtab_array_delete(value->strtab_array, value->strtab_size);
}
if (value->functab_value != NULL)
{
functab_delete(value->functab_value);
}
if (value->code != NULL)
{
bytecode_delete(value->code);
Expand All @@ -75,6 +83,10 @@ void module_close(module * value)
bytecode_func_addr(value->code);
bytecode_to_array(value->code, &value->code_arr, &value->code_size);
}
if (value->functab_value != NULL)
{
functab_close(value->functab_value);
}
}

void module_print(module * value)
Expand Down
Loading

0 comments on commit c658901

Please sign in to comment.