Is implicit string literal concatenation a good thing?

bearophile bearophileHUGS at lycos.com
Sun Feb 22 17:18:35 PST 2009


BCS:

> IIRC DMD doesn't always do the constant folding (Decent has a post processed 
> view that shows this in some cases) For instance, IIRC it only does left 
> most so this:
> char[] foo = "foo";
> char[] bar = foo ~ "bar" ~ "baz"
> doesn't get folded. And even if DMD were to start doing that one, there is 
> no requirement that another compiler also do it.

If there are guarantees that "abc" "def" are folded at compile time, then the same guarantees can be specified for "abc" ~ "def". I can't see a problem.

I have also compiled this code with DMD:

void main() {
    string foo = "foo";
    string bar = foo ~ "bar" ~ "baz";
}

Resulting asm, no optimizations:

L0:		push	EBP
		mov	EBP,ESP
		mov	EDX,FLAT:_DATA[0Ch]
		mov	EAX,FLAT:_DATA[08h]
		push	dword ptr FLAT:_DATA[01Ch]
		push	dword ptr FLAT:_DATA[018h]
		push	dword ptr FLAT:_DATA[02Ch]
		push	dword ptr FLAT:_DATA[028h]
		push	EDX
		push	EAX
		push	3
		mov	ECX,offset FLAT:_D11TypeInfo_Aa6__initZ
		push	ECX
		call	near ptr __d_arraycatnT
		xor	EAX,EAX
		add	ESP,020h
		pop	EBP
		ret


Resulting asm, with optimizations:

L0:		sub	ESP,0Ch
		mov	EAX,offset FLAT:_D11TypeInfo_Aa6__initZ
		push	dword ptr FLAT:_DATA[01Ch]
		push	dword ptr FLAT:_DATA[018h]
		push	dword ptr FLAT:_DATA[02Ch]
		push	dword ptr FLAT:_DATA[028h]
		push	dword ptr FLAT:_DATA[0Ch]
		push	dword ptr FLAT:_DATA[08h]
		push	3
		push	EAX
		call	near ptr __d_arraycatnT
		add	ESP,020h
		add	ESP,0Ch
		xor	EAX,EAX
		ret

I can see just one arraycatn, so the two string literals are folded at compile time, I think.

Bye,
bearophile



More information about the Digitalmars-d mailing list