Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuperForth Crashes - Attempts to create a type signature for generic for top-level code #35

Closed
TheRealMichaelWang opened this issue May 12, 2022 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@TheRealMichaelWang
Copy link
Owner

SuperForth crashes when trying to generate a type-signature while compiling top-level code(code that isn't encapsulated in a procedure).

The following code was used:
hashmap.txt:

abstract record hash_bucket<K, V>
final record empty_bucket<K, V> extends hash_bucket<K, V>
final record full_bucket<K, V> extends hash_bucket<K, V> {
	mustinit K key;
	mustinit V value;
}

final record hash_map<K, V> {
	mustinit array<hash_bucket<K, V>> buckets = [new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>,
						new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>, new empty_bucket<K, V>];
	mustinit proc<int, K> hasher;
}

global readonly auto hash_map_emplace = proc<K, V>(hash_map<K, V> map, K key, V value) return nothing {
	readonly auto rehash = proc<K, V>(hash_map<K, V> map, int new_size) return auto {
		array<hash_bucket<K, V>> old_buckets = map.buckets;		

		map.buckets = new hash_bucket<K, V>[new_size];
		int i = 0;
		while(i < new_size) {
			map.buckets[i] = new empty_bucket<K, V>;
			i = i + 1;
		}

		return old_buckets;
	};
	
	int bucket = map.hasher(key) % #map.buckets;
	while(bucket < #map.buckets) { $find the first empty bucket after computed hash_location
		if(map.buckets[bucket] is empty_bucket<any, any>) {
			map.buckets[bucket] = new full_bucket<K, V> {
				key = key;
				value = value;
			};
			return;
		}
		bucket = bucket + 1;
	}

	$no availible bukets were found if this code is reached
	auto reinsert_buckets = rehash<K, V>(map, bucket + 5);
	bucket = 0;
	while(bucket < #reinsert_buckets) {
		thisproc<K, V>(map, key, value);
		bucket = bucket + 1;
	}	
	thisproc<K, V>(map, key, value);
	return;
};

test.txt:

include "hashmap.txt";
include "io.sf";

auto mymap = new hash_map<int, array<char>> {
	hasher = proc(int i) return int {
		return i;
	};
};

hash_map_emplace<int, array<char>>(mymap, 5, "Michael");
hash_map_emplace<int, array<char>>(mymap, 2, "Michael");
hash_map_emplace<int, array<char>>(mymap, 7, "Tim");

println("HI!");

It can be reproduced by running:
superforth -cr -s test.txt.

@TheRealMichaelWang
Copy link
Owner Author

TheRealMichaelWang commented May 12, 2022

Here is the gdb output:

Program received signal SIGSEGV, Segmentation fault.
0x00005644aae0b9a1 in compile_type_to_machine (out_sig=0x5644ad059970, 
    type=..., compiler=0x7ffec20a6710, proc=0x0) at src/compiler.c:810
810         compiler_reg_t info_reg = TYPEARG_INFO_REG(type);
(gdb) p type
$1 = {type = TYPE_TYPEARG, sub_types = 0x0, sub_type_count = 0 '\000', 
  type_id = 0 '\000'}
(gdb) p proc
$2 = (ast_proc_t *) 0x0
(gdb) bt
#0  0x00005644aae0b9a1 in compile_type_to_machine (out_sig=0x5644ad059970, 
    type=..., compiler=0x7ffec20a6710, proc=0x0) at src/compiler.c:810
#1  0x00005644aae0bb16 in compile_type_to_machine (out_sig=0x5644ad05c990, 
    type=..., compiler=0x7ffec20a6710, proc=0x0) at src/compiler.c:829
#2  0x00005644aae0bbab in compiler_define_typesig (compiler=0x7ffec20a6710, 
    proc=0x0, type=...) at src/compiler.c:840
#3  0x00005644aae065d8 in compile_value (compiler=0x7ffec20a6710, value=..., 
    proc=0x0) at src/compiler.c:264
#4  0x00005644aae063a4 in compile_value (compiler=0x7ffec20a6710, value=..., 
    proc=0x0) at src/compiler.c:256
#5  0x00005644aae0672b in compile_value (compiler=0x7ffec20a6710, value=..., 
    proc=0x0) at src/compiler.c:268
#6  0x00005644aae0a79e in compile_code_block (compiler=0x7ffec20a6710, 
    code_block=..., proc=0x0, continue_ip=0, break_jumps=0x0, 
    break_jump_top=0x0) at src/compiler.c:556
#7  0x00005644aae0b628 in compile (compiler=0x7ffec20a6710, 
    safe_gc=0x7ffec20a6670, target_machine=0x7ffec20a6780, ast=0x7ffec20a66c0)
    at src/compiler.c:626
#8  0x00005644aae1cbd9 in main (argc=4, argv=0x7ffec20a7a88) at src/source.c:48
(gdb) 
#0  0x00005644aae0b9a1 in compile_type_to_machine (out_sig=0x5644ad059970, 
    type=..., compiler=0x7ffec20a6710, proc=0x0) at src/compiler.c:810
#1  0x00005644aae0bb16 in compile_type_to_machine (out_sig=0x5644ad05c990, 
    type=..., compiler=0x7ffec20a6710, proc=0x0) at src/compiler.c:829
#2  0x00005644aae0bbab in compiler_define_typesig (compiler=0x7ffec20a6710, 
    proc=0x0, type=...) at src/compiler.c:840
#3  0x00005644aae065d8 in compile_value (compiler=0x7ffec20a6710, value=..., 
    proc=0x0) at src/compiler.c:264
#4  0x00005644aae063a4 in compile_value (compiler=0x7ffec20a6710, value=..., 
    proc=0x0) at src/compiler.c:256
#5  0x00005644aae0672b in compile_value (compiler=0x7ffec20a6710, value=..., 
    proc=0x0) at src/compiler.c:268
#6  0x00005644aae0a79e in compile_code_block (compiler=0x7ffec20a6710, 
    code_block=..., proc=0x0, continue_ip=0, break_jumps=0x0, 
    break_jump_top=0x0) at src/compiler.c:556
#7  0x00005644aae0b628 in compile (compiler=0x7ffec20a6710, 
    safe_gc=0x7ffec20a6670, target_machine=0x7ffec20a6780, ast=0x7ffec20a66c0)
    at src/compiler.c:626
#8  0x00005644aae1cbd9 in main (argc=4, argv=0x7ffec20a7a88) at src/source.c:48

@TheRealMichaelWang
Copy link
Owner Author

After further investigation, it was determined that the cause of the bug is because the nested instances of types inside each ast value(the array of empty buckets) didn't have their type parameters substituted and replaced. Only the property type and top level type is substituted and replaced.

TheRealMichaelWang added a commit that referenced this issue May 14, 2022
@TheRealMichaelWang
Copy link
Owner Author

The issue seems to have been resolved for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants