Assignment & FUNCTION = expr

Syntax

Variable Assignment: variable = expression
Function Return: FUNCTION = expression
Source: pb_cg_emit_store_numeric_expr (pb_codegen.c:2251), pb_cg_emit_stmt (pb_codegen.c:~6630)

Description

The assignment operator (=) stores the result of an expression into a variable. The compiler handles different variable types (LOCAL, STATIC, GLOBAL, BYREF parameters, TYPE fields, and array elements) and automatically generates the correct addressing mode for each. For functions, FUNCTION = expression sets the return value, which is loaded into RAX (integer), XMM0 (float), or RAX:RDX (string) during the function epilogue.

Parameters

ParameterDescription
variableThe target variable name. Can be a LOCAL, STATIC, GLOBAL, BYREF parameter, TYPE field, or array element.
expressionAny numeric, string, or pointer expression whose result is stored into the variable.
FUNCTION = valueSets the return value of a FUNCTION. The value is stored in a dedicated return slot in the stack frame and loaded during the epilogue.

Example

FUNCTION PBMAIN() AS LONG
    LOCAL x AS LONG
    LOCAL y AS LONG
    LOCAL name AS STRING
    
    ' Simple assignment
    x = 10
    y = x * 2 + 5
    
    ' String assignment
    name = "Hello"
    
    ' Function return via FUNCTION =
    FUNCTION = x + y
END FUNCTION

Generated Assembly

Variable = expr

Syntax: variable = expression
Source: pb_cg_emit_store_numeric_expr (pb_codegen.c:2251)

Stack Local (numeric)

; Evaluate expression -> RAX
mov    [rbp - offset], rax           ; store to stack frame

BYREF Parameter (pointer dereference)

mov    r11, [rbp - offset]           ; load pointer from param slot
mov    [r11], rax                    ; store through pointer

STATIC / GLOBAL variable

mov    [label], rax                  ; direct static storage

TYPE Field

mov    [rbp - written_offset], r11   ; save field address
mov    r11, [rbp - written_offset]
mov    [r11], rax                    ; store value at field address

Array Element

; pb_cg_emit_array_address -> r11 = element address
mov    [rbp - written_offset], r11
mov    r11, [rbp - written_offset]
mov    [r11], rax                    ; a(i) = expr

FUNCTION = expr (Return Value)

Syntax: FUNCTION = expression
Source: pb_cg_emit_stmt (pb_codegen.c:~6630)

Integer Return

; Evaluate expression -> RAX
mov    [rbp - return_offset], rax    ; store to return slot
; On function exit:
mov    rax, [rbp - return_offset]    ; load return value
add    rsp, frame_size
pop    rbp
ret

Float Return

movsd  [rbp - return_offset], xmm0   ; store float to return slot
; On function exit:
movsd  xmm0, [rbp - return_offset]   ; load to XMM0 (Win64 FP return)
add    rsp, frame_size
pop    rbp
ret

String Return

lea    r11, [rbp - return_offset]    ; descriptor address
; pb_cg_emit_string_expr_to_descriptor (complex string copy)
; On exit:
mov    rax, [rbp - return_offset]          ; string data pointer
mov    rdx, [rbp - return_length_offset]   ; string length
add    rsp, frame_size
pop    rbp
ret

PBMAIN Return

mov    rcx, [rbp - return_offset]    ; exit code
call   [__imp_ExitProcess]
xor    rax, rax                      ; unreachable
add    rsp, frame_size
pop    rbp
ret