Skip to content

Review and simplify/improve alloc_str calls in the C backend #283

@osa1

Description

@osa1

Currently if I have a string "Hi" in a program this is the C we generate:

alloc_str("Hi", 2)

alloc_str allocates a new memory and copies the string:

fir/src/to_c.rs

Lines 156 to 162 in d0047fc

// Allocate string from bytes
static Str alloc_str(const char* bytes, size_t len) {{
uint8_t* data = malloc(len);
memcpy(data, bytes, len);
Array_U8 arr = {{ .data_ptr = data, .len = len }};
return (Str){{ ._0 = arr }};
}}

Instead of allocating a new memory we could reuse the static memory of the string literal as the data pointer here.

We should review all alloc_str calls to check whether the argument can be used as the data pointer, and whether we're properly freeing the argument after calling alloc_str.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersreference implIssues about the reference implementation of Fir, in Rust. Mainly the interpreter and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions