Stage 92: let's concatenate!

Follow the development of the SharpBASIC compiler and share your thoughts and ideas.
Locked
User avatar
frank
Site Admin
Posts: 40
Joined: Sun Nov 21, 2021 12:04 pm
Location: Netherlands
Contact:

Stage 92: let's concatenate!

Post by frank »

This stage is a major milestone in the development of the SharpBASIC compiler. After implementing string assignment and passing managed by a reference count system, the final step of string support is concatenation. With assignment and passing in place, this was relatively easy.

Example of concatenating strings:

Code: Select all

decl func Title():str;

const C = "SharpBASIC is here!";
dim s:str;

main do
  s = "Hello " + "world" + ", " + Title();
  print(s);
end;

func Title():str
do
  Title = C;
end;
Output: Hello world, SharpBASIC is here!

generated asm output of the above code:

Code: Select all

_start:
	call    _sb_create_strdesc
	mov     [_I85], eax
	mov     ebx, _C5
	call    _sb_creassign_constr
	mov     esi, eax
	mov     ebx, _C6
	call    _sb_concat_constr
	mov     esi, eax
	mov     ebx, _C7
	call    _sb_concat_constr
	mov     esi, eax
	call    _I83
	call    _sb_dstref
	call    _sb_concat_str
	mov     esi, eax
	mov     edi, dword [_I85]
	call    _sb_assign_str
	mov     [_I85], eax
	mov     eax, dword [_I85]
	call    _sb_load_strdesc
	call    _sb_printl
	mov     eax, dword [_I85]
	call    _sb_testd_strefdd

_end:
	mov     ebx, 0
	mov     eax, 1
	int     80h

_I83:
	push    ebp
	mov     ebp, esp
	sub     esp, 4
	call    _sb_create_strdesc
	mov     dword [ebp - 4], eax
	mov     ebx, _C4
	mov     eax, dword [ebp - 4]
	call    _sb_assign_constr
	mov     [ebp - 4], eax
._L0:
	mov     eax, dword [ebp - 4]
	mov     esp, ebp
	pop     ebp
	ret
Keep it simple!
Locked