oberon-07-compiler
oberon-07-compiler copied to clipboard
Passing values to C library
Hi @AntKrotov! How are you? I hope you are doing well. First of all, I would like to thank you for your great work in this project! I have been looking you code and is very well written and organized. I'm implementing an idea for compiling to Elf relocatable files. My main goal is to write code in Oberon that I can later link with other C libraries statically. I'm pretty happy with it and if you have interest, I can share the implementation :) But for now, I'm trying to understand the behavior for passing values from Oberon to C. I'll attach the entire files. The tests were done on Linux32 and Linux64.
Here are the types and procedure variables:
TYPE
Color = RECORD r, g, b, a: BYTE END;
ColorPtr = POINTER TO Color;
Cptr = INTEGER;
VAR
testParams1: PROCEDURE [linux] (c: INTEGER);
testParams2: PROCEDURE [linux] (c: Cptr);
testParams3: PROCEDURE [linux] (c: INTEGER);
testParams4: PROCEDURE [linux] (c: ColorPtr);
testParams5: PROCEDURE [linux] (c: SET);
Here is the procedure which is calling the C code:
PROCEDURE main();
VAR
c1: INTEGER;
c2: Color;
c3: ColorPtr;
c4: SET;
BEGIN
c1 := 0FFFF00FFH;
c2.r := 255; c2.g := 0; c2.b := 0; c2.a := 0;
NEW(c3); c3^ := c2;
c4 := {0..7, 16..31};
testParams1(c1);
testParams2(SYSTEM.ADR(c2));
testParams3(c1);
testParams4(c3);
testParams5(c4);
END main;
Here is the C program:
#include <stdio.h>
typedef struct Color {
unsigned char r, g, b, a;
} Color;
void testParams1(Color c) {
printf("color1: %d, %d, %d, %d\n", c.r, c.g, c.b, c.a);
}
void testParams2(Color* c) {
printf("color2: %d, %d, %d, %d\n", c->r, c->g, c->b, c->a);
}
void testParams3(int c) {
printf("color3: %d, %d, %d, %d\n",
c & 0xff000000, c & 0x00ff0000 , c & 0x0000ff00, c & 0x000000ff);
}
void testParams4(Color* c) {
printf("color4: %d, %d, %d, %d\n", c->r, c->g, c->b, c->a);
}
void testParams5(int c) {
printf("color5: %d, %d, %d, %d\n",
c & 0xff000000, c & 0x00ff0000, c & 0x0000ff00, c & 0x000000ff);
}
And here is the output of the C program:
color1: 255, 0, 255, 255 color2: 255, 0, 0, 0 color3: -16777216, 16711680, 0, 255 color4: 255, 0, 0, 0 color5: -16777216, 16711680, 0, 255
The "color1" conversion kind of work, by apparently the endianness is incorrect. "color2" and "color4" worked as expected. "color3" and "color5" I did not understand what is happening. Maybe is related to endianness as well.
Can you help in identifying what I'm doing wrong?
Thanks in advance! Evandro.