Code Monkey home page Code Monkey logo

dlang-mos-hello-world's Introduction

dlang-mos-hello-world

LDC2 (2.108.1/master) using LLVM-MOS (v18) backend

Build ldc2 + llvm-mos or run

$ docker run --rm -it -v $(pwd):/work -w /work kassany/ldc-mos:latest bash

image: https://hub.docker.com/r/kassany/ldc-mos

dlang-mos-hello-world's People

Contributors

kassane avatar

Stargazers

 avatar  avatar

Watchers

 avatar

dlang-mos-hello-world's Issues

Struct size mismatch for mos6502

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.