So I am implementing you could say subset of C compiler, and there is one feature which gives me headache. So I would like some ideas how to solve it.
In my prog. language are all arguments passed by value including strings, whose are declared like this:
string str;
And my issue is that, as you can see, I don't know string size during its declaration(difference from C), so when i am generating assembly i don't know how big space to create on stack for it. If I have code like this:
string str;
int number;
str = something;
I don't know how to allocate correct space for string str, when it later can be assigned practicaly anythign. And last condition is, that I can't use heap.
Thanks, and sorry for my bad english.
Edit:
Thanks for the answers. From the responses it seems to me, that biggest problem will be reallocation string if it gets bigger on stack in case when there is already something behind this string, the simplest solution i think would be create new space on stack and the old one let be until local scope will be disposed... It would be wasting, yes
I assume you have some sort of
dup_string
routine that duplicates a string in a new memory block. That dup routine must be aware that the source string has an unkown length, that will be only determined while the copying is being performed, something like:So you can use this function transparently by using it in your caller code and your calle epilog, like this...
Generated assembly code would be like this (example using IA32 code, and C calling convention):