Skip to content

Conversation

@N1ark
Copy link
Contributor

@N1ark N1ark commented Jul 24, 2025

Motivations

For the interpreter I'm writing at https://github.com/soteria-tools/soteria, I need to special case Literal::Str quite heavily, since a string is not "just" a literal, but rather a reference to an allocation containing a string; this means that a Literal::Str actually hides an allocation, unlike all other literals.

It would be nice if the representation of strings was less fake in some way, by making the existence of this allocation and reference clearer.

This PR

Add a --string-globals flag, that removes all string literals and replaces them with a reference to a array of u8. More precisely, the following substitution happens:

let s = "hello";

into

const string_constant::<"hello"> = {
	let arr: [u8; 5] = [105, 101, 108, 108, 111];
    let arr_ref: &[u8; 5] = &arr;
    let slice: &[u8] = <unsize>(move arr_ref);
    let str: &'static str = <transmute>(move slice);
    return str
}

let s = string_constant::<"hello">

Right now the arr value is defined using a RawConstantExpr::RawMemory, for a more compact representation; in a previous commit I did it with an array aggregate, but that is much more verbose and less compact.
Another option I haven't done, that may be better, is that we decide that when --string-globals is toggled, Literal::Str doesn't represent a &str but a str (or rather, a [u8; N], where N is the length of the string). This makes the [U]LLBC more readable and is a more compact representation, but it means we have a value that has two meanings depending on a flag, which is maybe not ideal.

Let me know what you think; I'm happy to change this as needed :)

A last option is that we just change the meaning of Literal::Str to be the actual [u8] behind it, and we convert all Literal::Strs to ConstantExpr::Ref(ConstantExpr(Literal::Str)); however that means you have locals storing the str, which is an unsized type, so that doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant