Skip to content

Bizarre output when generating bytecode of simple input #55

@mundusnine

Description

@mundusnine

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions