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

Struct size mismatch for mos6502 #1

Open
kassane opened this issue May 31, 2024 · 0 comments
Open

Struct size mismatch for mos6502 #1

kassane opened this issue May 31, 2024 · 0 comments
Labels
help wanted Extra attention is needed wontfix This will not be worked on

Comments

@kassane
Copy link
Owner

kassane commented May 31, 2024

Currently the same LLVM-MOS v18 Backend is used in zig and ldc2 (dlang). However, when building the new soa_example.d file, it can be seen that the layout of the SOA structure has a size of i32 bit and not i16 bit.

I have tried modifying the compiler bitness on the ldc2 side (not dmd frontend), by adding mos-target to the compiler driver. However, no change in the result.

src: https://github.com/kassane/dlang-mos-hello-world/blob/main/c64/soa/src/soa_example.d

code ported to C
#include <stdio.h>

typedef struct {
    unsigned int c;
    unsigned int d;
} TransparentStruct;

typedef struct {
    TransparentStruct data[5];
    size_t length;
} StructOfArray;

void clear(StructOfArray *soa) {
    soa->length = 0;
}

size_t capacity(const StructOfArray *soa) {
    return sizeof(soa->data) / sizeof(soa->data[0]);
}

size_t size(const StructOfArray *soa) {
    return soa->length;
}

int main() {
    StructOfArray soa = {
        .data = {
            { 'a', 'b' }, { 'c', 'd' },
            { 'e', 'f' }, { 'g', 'h' },
            { 'i', 'j' }
        },
        .length = 5
    };

    for (size_t idx = 0; idx < sizeof(soa.data) / sizeof(soa.data[0]); idx++) {
        printf("c[%zu].c: %d\n", idx, soa.data[idx].c);
        printf("c[%zu].d: %d\n", idx, soa.data[idx].d);
    }

    clear(&soa);
    printf("Cleared size: %zu\n", size(&soa));
    printf("Capacity: %zu\n", capacity(&soa));

    return 0;
}
build commands
$ zig cc \
    -target mos-freestanding-none \
    src/soa_example.c \
    -I/opt/llvm-mos-sdk/mos-platform/common/include \
    -Oz \
    -s \
    -c \
    -S \
    -emit-llvm \
    -o soa_zigcc.ll

$ ldmd2 -i \
    -mtriple=mos \
    src/soa_example.c \
    -I/opt/llvm-mos-sdk/mos-platform/common/include \
    -release \
    -Oz \
    -betterC \
    --output-ll \
    -of=soa_ldc.ll \
    -gcc=/opt/llvm-mos/bin/mos-c64-clang \
    -linker=/opt/llvm-mos/bin/ld.lld

ldc2 output:

; ModuleID = 'src/soa_example.c'
source_filename = "src/soa_example.c"
target datalayout = "e-m:e-p:16:8-p1:8:8-i16:8-i32:8-i64:8-f32:8-f64:8-a:8-Fi8-n8"
target triple = "mos"

%soa_example.StructOfArray = type { [5 x %soa_example.TransparentStruct], i32 }
%soa_example.TransparentStruct = type { i32, i32 }

@.str = private unnamed_addr constant [14 x i8] c"c[%zu].c: %d\0A\00" ; [#uses = 5]
@.str.1 = private unnamed_addr constant [14 x i8] c"c[%zu].d: %d\0A\00" ; [#uses = 5]
@.str.2 = private unnamed_addr constant [19 x i8] c"Cleared size: %zu\0A\00" ; [#uses = 1]
@.str.3 = private unnamed_addr constant [15 x i8] c"Capacity: %zu\0A\00" ; [#uses = 1]

zig output:

; ModuleID = 'src/soa_example.c'
source_filename = "src/soa_example.c"
target datalayout = "e-m:e-p:16:8-p1:8:8-i16:8-i32:8-i64:8-f32:8-f64:8-a:8-Fi8-n8"
target triple = "mos-unknown-unknown-unknown"

%struct.StructOfArray = type { [5 x %struct.TransparentStruct], i16 }
%struct.TransparentStruct = type { i16, i16 }

@.str = private unnamed_addr constant [14 x i8] c"c[%zu].c: %d\0A\00", align 1
@.str.1 = private unnamed_addr constant [14 x i8] c"c[%zu].d: %d\0A\00", align 1
@.str.2 = private unnamed_addr constant [19 x i8] c"Cleared size: %zu\0A\00", align 1
@.str.3 = private unnamed_addr constant [15 x i8] c"Capacity: %zu\0A\00", align 1

References

@kassane kassane added help wanted Extra attention is needed wontfix This will not be worked on labels May 31, 2024
@kassane kassane changed the title Struct size mismath for mos6502 Struct size mismatch for mos6502 May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant