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

Bizarre output when generating bytecode of simple input #55

Open
mundusnine opened this issue Mar 10, 2025 · 1 comment
Open

Bizarre output when generating bytecode of simple input #55

mundusnine opened this issue Mar 10, 2025 · 1 comment

Comments

@mundusnine
Copy link

mundusnine commented Mar 10, 2025

I am using the modified lcc used in q3vm. I compared the source for the bytecode generation and no changes in the bytecode are present except for not enabling doubles and converting them to floats and popping after a CALL OP.

Image

Image

The issue

When generating bytecode for this code:

float add_u42f4_test(unsigned int num_a,float num_b){
   return num_a + num_b;
}

lcc generates this:

export add_u42f4_test
code
proc add_u42f4_test 4 0
file "./tests/lcc/add_u42f4_test.c"
line 1
;1:float add_u42f4_test(unsigned int num_a,float num_b){
line 2
;2:   return num_a + num_b;
ADDRLP4 0
ADDRFP4 0
INDIRU4
ASGNU4
CNSTF4 1073741824
ADDRLP4 0
INDIRU4
CNSTI4 1
RSHU4
CVUI4 4
CVIF4 4
MULF4
ADDRLP4 0
INDIRU4
CNSTU4 1
BANDU4
CVUI4 4
CVIF4 4
ADDF4
ADDRFP4 4
INDIRF4
ADDF4
RETF4
LABELV $1
endproc add_u42f4_test 4 0

I naively generate with my compiler(I basically use chibicc to parse and give me the AST and generate the bytecode from the AST):

export add_u42f4_test
code
proc add_u42f4_test 0 0
ADDRFP4 0
INDIRU4
CVUI4 4
ADDRFP4 4
INDIRF4
ADDF4
RETF4
LABELV $1
endproc add_u42f4_test 0 0

The odd things I see are that lcc creates a local variable(none are defined in the c source), it uses a constant f value 1073741824 does multiplies when a simple add is expected. When running in the q3vm it of course gives me different output:

[ERROR] Got:
[ERROR] add_u42f4_test 59.000000 :./chibicc_output.txt:6
[ERROR] ~~~~~~~~~~~~~~~^
[ERROR] Expected:
[ERROR] add_u42f4_test 1113849856.000000 :./lcc_output.txt:6
[ERROR] ~~~~~~~~~~~~~~~^

The c source that calls the code:

#include "../q3vm/scripts/bg_lib.h"

void main_init(void){
	//Other tests
	{
		char temp[64] = {0};
		float res = add_u42f4_test(57,2);
		snprintf(temp,64,"add_u42f4_test %f\n",res);
		trap_Printf(temp);
	}
        //Other tests
	exit();
}

Here is the repo with all the tests that I have made.

@ksherlock
Copy link

1073741824 is the bitwise representation of 2.0 (as a float). This is where that code is coming from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants