// File: pixlark[P]Badge/src/main.cc
// Function: work_from_source(char const*)
void work_from_source(const char * path)
{
	/*
	if (strcmp(path, "-") == 0) {
		// Read from stdin
		String_Builder builder;
		const int chunk_size = 64;
		char buf[chunk_size];
		
		size_t read;
		while ((read = fread(buf, sizeof(char), chunk_size, stdin)) == chunk_size) {
			builder.append_size(buf, chunk_size);
		}
		if (ferror(stdin)) {
			fatal("Error reading from stdin");
		}
		assert(read <= chunk_size);
		builder.append_size(buf, read);
		source = builder.final_string();
		}*/

	Blocks blocks;
	blocks.init();
	const char * source = load_and_compile_file(&blocks, path);
	if (!source) {
		fatal("File '%s' does not exist!", path);
	}

	#if OUTPUT_BYTECODE
	for (int i = 0; i < blocks.blocks.size; i++) {
		auto block = blocks.blocks[i];
		auto size = blocks.sizes[i];
		printf("Block %d:\n", i);
		for (int j = 0; j < size; j++) {
			char * s = block[j].to_string();
			printf("%02d %s\n", j, s);
			free(s);
		}
	}
	#endif
	
	auto export_scope = Environment::alloc();
	
	// Finally, just run our bytecode through the VM, starting with
	// `block_reference` 0
	List<VM> vm_stack;
	vm_stack.alloc();

	{
		VM vm;
		vm.init(&blocks, export_scope, 0);
		vm_stack.push(vm);
	}

	while (vm_stack.size > 0) {
		auto vm = &vm_stack[vm_stack.size - 1];
		auto response = vm->step();
		switch (response) {
		case VM_OK:
			break;
		case VM_HALTED: {
			vm->destroy();
			vm_stack.pop();
		} break;
		case VM_SWITCH: {
			VM new_vm;
			new_vm.init(&blocks, export_scope, vm->block_reference_to_push);
			vm_stack.push(new_vm);
		} break;
		default:
			assert(false);
		}

		#if DEBUG_OUTPUT
		vm->print_debug_info();
		#endif
		
		// This should ALWAYS run before the loop breaks, that way
		// anything that needs to get cleaned up, will be.
		#if COLLECTION
		do {
			#if RELEASE
			if (!GC::should_collect()) {
				break;
			}
			GC::reset_heuristics();
			#endif
			GC::unmark_all();
			for (int i = 0; i < vm_stack.size; i++) {
				vm_stack[i].mark_reachable();
			}
			GC::free_unmarked();
		} while(0);
		#endif
	}
	
	assert(vm_stack.size == 0);
	vm_stack.dealloc();
	
	blocks.destroy();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x450, %rsp            # imm = 0x450
movq %rdi, -0x8(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x80d0
movq -0x8(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x61f0
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x6472
movq -0x8(%rbp), %rsi
leaq 0xa637(%rip), %rdi      # 0x10aa2
movb $0x0, %al
callq 0x1c50
callq 0x8140
movq %rax, -0x68(%rbp)
movq 0x10d5e(%rip), %rax     # 0x171e0
movq %rax, -0xa0(%rbp)
movq 0x10d58(%rip), %rax     # 0x171e8
movq %rax, -0x98(%rbp)
movq -0xa0(%rbp), %rsi
movq -0x98(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x8190
movq -0x68(%rbp), %rdx
leaq -0x138(%rbp), %rdi
leaq -0x58(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
callq 0x81f0
leaq -0x1d0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
movl $0x98, %edx
callq 0x10e0
movq %rsp, %rdi
movl $0x13, %ecx
leaq -0x1d0(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x90(%rbp), %rdi
callq 0x82d0
cmpq $0x0, -0x88(%rbp)
jbe 0x6661
movq -0x88(%rbp), %rsi
decq %rsi
leaq -0x90(%rbp), %rdi
callq 0x8330
movq %rax, -0x1d8(%rbp)
movq -0x1d8(%rbp), %rdi
callq 0x8390
movl %eax, -0x1dc(%rbp)
movl -0x1dc(%rbp), %eax
movl %eax, -0x3b0(%rbp)
testl %eax, %eax
je 0x656b
jmp 0x654e
movl -0x3b0(%rbp), %eax
subl $0x1, %eax
je 0x6570
jmp 0x655b
movl -0x3b0(%rbp), %eax
subl $0x2, %eax
je 0x6591
jmp 0x65eb
jmp 0x660a
movq -0x1d8(%rbp), %rdi
callq 0x9df0
leaq -0x278(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x9e60
jmp 0x660a
movq -0x68(%rbp), %rdx
movq -0x1d8(%rbp), %rax
movq 0x90(%rax), %rcx
leaq -0x310(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x81f0
leaq -0x3a8(%rbp), %rdi
leaq -0x310(%rbp), %rsi
movl $0x98, %edx
callq 0x10e0
movq %rsp, %rdi
movl $0x13, %ecx
leaq -0x3a8(%rbp), %rsi
rep  movsq (%rsi), %es:(%rdi)
leaq -0x90(%rbp), %rdi
callq 0x82d0
jmp 0x660a
leaq 0xa00a(%rip), %rdi      # 0x105fc
leaq 0xa4c3(%rip), %rsi      # 0x10abc
movl $0x8d, %edx
leaq 0xa4c3(%rip), %rcx      # 0x10ac8
callq 0x10d0
jmp 0x660c
callq 0x51e0
movl $0x0, -0x3ac(%rbp)
movslq -0x3ac(%rbp), %rax
cmpq -0x88(%rbp), %rax
jae 0x6657
movslq -0x3ac(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x8330
movq %rax, %rdi
callq 0x9ef0
movl -0x3ac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3ac(%rbp)
jmp 0x661b
callq 0x5230
jmp 0x64ff
cmpq $0x0, -0x88(%rbp)
jne 0x666d
jmp 0x668c
leaq 0xa478(%rip), %rdi      # 0x10aec
leaq 0xa441(%rip), %rsi      # 0x10abc
movl $0xa7, %edx
leaq 0xa441(%rip), %rcx      # 0x10ac8
callq 0x10d0
leaq -0x90(%rbp), %rdi
callq 0x9fa0
leaq -0x58(%rbp), %rdi
callq 0x9fe0
addq $0x450, %rsp            # imm = 0x450
popq %rbp
retq
nopw (%rax,%rax)