C compiler

PM development, programming, hacking and all that fun stuff.
Lupin
Team Pokémé
Posts: 94
Joined: January 10th, 2005, 13:06

Re: C compiler

Post by Lupin » May 27th, 2011, 11:12

Is there a way _not_ to inline assembler functions? All ASM functions are automatically inlined which is not always a good idea

Lupin
Team Pokémé
Posts: 94
Joined: January 10th, 2005, 13:06

Re: C compiler

Post by Lupin » May 27th, 2011, 11:33

Code: Select all

void test(char* a) {
	if(*a != 0) {}
}

Code: Select all

;	.global	test
test:
	mov	ba,[sp+3]
	mov	a,[ba]
	ex	ba,a
	mov	x,ba
vbcc___l45:
vbcc___l46:
vbcc___l44:
	ret
mov a,[ba] does not exist. There is no instruction to ever use BA for addressing.

Which registers can be used in inline asm? Which must be saved to stack? Functions always return their return value to x, right?

Lupin
Team Pokémé
Posts: 94
Joined: January 10th, 2005, 13:06

Re: C compiler

Post by Lupin » May 27th, 2011, 11:44

Code: Select all

int main() {
char str1[20] = {0, 10, 11, 0x0};
}

Code: Select all

main:
	sub	sp,20
	mov	ba,vbcc___l60
	mov	[sp+0],ba
vbcc___l59:
	add	sp,20
	ret
vbcc___l60:
	.db	0
	.db	10
	.db	11
	.db	0
	.db	0, 0, 0, 0, 0, 0, 0, 0
	.db	0, 0, 0, 0, 0, 0, 0, 0
Initialized arrays don't work... :(
It reserves stack space (20 bytes) for the array, but then it moves the ROM address of the initialized data to the stack instead of copying the data to the stack.

Lupin
Team Pokémé
Posts: 94
Joined: January 10th, 2005, 13:06

Re: C compiler

Post by Lupin » May 27th, 2011, 12:08

Small work-around:

Code: Select all

int strlen(__reg("x") char*) =
	"\t mov hl, x"
	"\t dec x\n"\
	"_nextchr:\t inc x\n"\
	"\t mov a, [x] \n"\
	"\t cmp a, 0 \n"\
	"\t jnz _nextchr\n"\
	"\t sub x, hl\n";

// memcpy
void memcpy(__reg("x") char*, __reg("y") char*, int size) =
	"\t mov BA, [SP+0] \n"\
	"\t tst a, 0x1     \n"\
	"\t mov HL, BA     \n"\
	"\t jz  _cpyloop   \n"\
	"\t mov a, [y]     \n"\
	"\t mov [x], a     \n"\
	"\t inc x          \n"\
	"\t inc y          \n"\
	"\t dec HL         \n"\
	"\t cmp HL, 0      \n"\
	"\t jz _end        \n"\
	"_cpyloop:\t mov BA, [y]    \n"\
	"\t mov [x], BA    \n"\
	"\t mov BA, 2      \n"\
	"\t add x, BA      \n"\
	"\t add y, BA      \n"\
	"\t sub HL, BA     \n"\
	"\t cmp HL, 0      \n"\
	"\t jnz _cpyloop   \n"\
	"_end:\n";

int main()
{
	int i, a;
	char str1[20] = {1, 10, 9, 3, 4, 0};
	char* src = (char*)((str1[1]<<8) + str1[0]);
	memcpy(str1, src, sizeof(str1));
	
	for(i=0; i<strlen(str1); i++)
		print_digit(3 + 6 * i, 6, str1[i]);
	
	return 0;
}
This will output 1A934 to display using your print_digit function...

zoranc
Posts: 297
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc » May 27th, 2011, 19:33

Heh, Lupin you made this a burning topic. Ok easy things first: the inline assembly can allways be wrapped in a C function, like this:

int do_something_asm(__reg("x") int) = "...";

int do_something(int parm) { return do_something_asm(parm); }

Now the function calling convention is still work in progress. Currently reg X and Y are used by the front-end so you have to save them. Where ba and hl are for the backend so you can use them without need to save them. Return value is placed in reg X in case that it fits there. But please be aware those are yet not final. Also I have to check if the compiler saves X if it is declared as a parameter to asm function (it should!).

The [ba] addressing mode is another problem I'm aware of from the bad code generator. It was rather a quick hack than properly implemented. That's why I need to rework it. Probably would be able to do it in couple of full working days but unfortunately don't have them right now.

Now the stack array is a new one. It seems to come from not fully implemented functionality in the backend. I would really like to have some test suite available like that of the SDCC but it's probably too early for that too.

zoranc
Posts: 297
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc » June 3rd, 2011, 03:04

Minor update - version 0.010. Some problems fixed.

Main issues to address - proper handling of the char type. Option to have the static variables in RAM.
Attachments
vbccpm_0010.zip
(289.48 KiB) Downloaded 381 times

zoranc
Posts: 297
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc » June 3rd, 2011, 13:44

Version 0.011 here. Added support for global variables in RAM. Unfortunately they are not initialized at the moment. Also char/short types are kind of very buggy. But ints and pointers should mostly work. Look in the demo file for some of the features of the compiler.
Attachments
vbccpm_0011.zip
(289.78 KiB) Downloaded 373 times

Lupin
Team Pokémé
Posts: 94
Joined: January 10th, 2005, 13:06

Re: C compiler

Post by Lupin » June 5th, 2011, 01:34

Just coded a C version of my 6 color LCD demo... It can either be compiled with or without inline ASM. It's of course a lot slower without inline ASM. But even with inline ASM it's not looking as good as the ASM only version.

Still need to use a workaround to write to memory addresses (wcp function).

Why the "__rom" prefix for ROM arrays? I think it's common to move everything with "const" prefix into ROM.
Attachments
6color_c.zip
6 color demo in C
(34.63 KiB) Downloaded 406 times

zoranc
Posts: 297
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc » June 5th, 2011, 04:43

some fix with mul/div by const power of 2.

added a makefile.

added font created by friend of me who gave me permission to use it.

enjoy!
Attachments
vbccpm_0012.zip
Version 0.012
(292.71 KiB) Downloaded 377 times

zoranc
Posts: 297
Joined: August 13th, 2010, 18:13
Contact:

Re: C compiler

Post by zoranc » June 7th, 2011, 02:38

Another day another release. This time chars are supported too! :D

@Lupin: your 6-color demo now runs in pure C too but of course it looks ugly with that speed.
Attachments
vbccpm_0013.zip
(292.55 KiB) Downloaded 384 times

Post Reply