Sign in Join
Wiki copytemporary

WebAssembly/Globals: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11WebAssembly supports a feature, called "global variables" or just "globals". They differ from global variables in Pascal. This page discusses the possibility of extending FPC, in order to be able to declare WebAssembly globals from Pascal code.
22
33## Differences between Pascal global variables and WebAssembly globals
44
55WebAssembly globals have the following restrictions:
66
77- They can be only one of the 4 WebAssembly basic types - int32, int64, float32 or float64.
88- Their address cannot be taken. They don't exist anywhere in linear memory.
99- In a multithreaded environment, they are usually not shared between threads, i.e. they behave like threadvars.
1010
1111These characteristics make them somewhat comparable to CPU or FPU registers on other architectures. However, they differ from registers in the following way:
1212
1313- There is an unlimited number of them.
1414- They can be imported, exported, or just declared in a module (i.e. neither imported, nor exported, just declared and used by the module).
1515
1616In WebAssembly MVP (the original version of WebAssembly), they could only exist in the third state - neither imported, nor exported, but importing and exporting them from the module is a commonly implemented extension: [1](https://github.com/WebAssembly/mutable-global/blob/master/proposals/mutable-global/Overview.md)
1717
1818## Special WebAssembly globals
1919
2020Free Pascal already creates and uses some WebAssembly globals, which are considered special and are part of the [Basic C ABI](https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md), which Free Pascal supports.
2121
2222### __stack_pointer
2323
2424WebAssembly has an evaluation stack, which holds local variables, stack frames and temporary values. However, this stack is not visible in linear memory. This means that you cannot take the address of a value on this stack. Another restriction is that it can only hold the 4 basic WebAssembly types (int32, int64, float32, float64). To avoid these restrictions, Free Pascal maintains a second stack in linear memory. The WebAssembly global *__stack_pointer* points to the top of this stack. It is always 16-byte aligned.
2525
2626### TLS-related globals
2727
2828When multithreading is enabled, the linker creates the following globals:
2929
3030- *__tls_size* - the total size of the thread local block of the program. This is the sum of the sizes of all threadvars, plus padding.
3131- *__tls_align* - the alignment requirement for the thread local block.
3232- *__tls_base* - initialized to 0, but can be set by the function *__wasm_init_tls*, which is created by the linker. All threadvar accesses, created by the compiler take the address of this variable and add an offset.
3333
3434## Pascal syntax proposal
3535
3636WebAssembly globals are not yet implemented in Free Pascal. This section discusses the syntax for a possible language extension for supporting them.
3737
38-`var`\
39-`  a: LongInt; wasmglobal;  // FPC's name mangling applies`\
40-`  aa: LongInt = 5; wasmglobal;  // initialized global`\
41-`  b: LongInt; wasmglobal name 'x';  // FPC's name mangling is not applied`\
42-`  c: LongInt; wasmglobal; external name 'q';  // external, declared in a different unit, or .o module`\
43-`  d: LongInt; wasmglobal; import name 'ddd';  // an import for the final .wasm module`\
38+``` pascal
39+var
40+ a: LongInt; wasmglobal; // FPC's name mangling applies
41+ aa: LongInt = 5; wasmglobal; // initialized global
42+ b: LongInt; wasmglobal name 'x'; // FPC's name mangling is not applied
43+ c: LongInt; wasmglobal; external name 'q'; // external, declared in a different unit, or .o module
44+ d: LongInt; wasmglobal; import name 'ddd'; // an import for the final .wasm module
4445
45-`const`\
46-`  ccc: LongInt = 10; wasmglobal;  // immutable global`
46+const
47+ ccc: LongInt = 10; wasmglobal; // immutable global
48+```
4749
4850TODO: can we achieve this without introducing a new keyword?
4951
5052Introducing a new keyword can break existing code, if it happens to use the new keyword as a variable name, for example, the following code would break:
5153
52-`var`\
53-`  a: Longint;`\
54-`  wasmglobal: Boolean;`
54+``` pascal
55+var
56+ a: Longint;
57+ wasmglobal: Boolean;
58+```
5559
5660Proposed by Jonas:
5761
58-`var`\
59-`  a: LongInt; section 'WebAssembly.Global';`
62+``` pascal
63+var
64+ a: LongInt; section 'WebAssembly.Global';
65+```
6066
6167Key here is the fact that the 'section' keyword already exists.
6268
6369Supported types:
6470
6571- LongInt (32-bit signed int) - mapped to i32
6672- LongWord (32-bit unsigned int) - mapped to i32
6773- Int64 (64-bit signed int) - mapped to i64
6874- QWord (64-bit unsigned int) - mapped to i64
6975- Single - mapped to f32
7076- Double - mapped to f64
7177- Pointer (typed or untyped) - mapped to i32
7278
7379Unsupported types:
7480
7581- enum
7682- set
7783- string
7884- class
7985- object
8086- record
8187- smallint/shortint/byte/word
8288- char
8389- boolean
8490- array
8591- variant
8692
8793Some of the unsupported types can be supported in the future (good candidates are smallint/shortint/byte/word, boolean, char and enums). However, we generally don't want to cram all possible Pascal types. Wasmglobals are a very special feature, used for communicating with the WebAssembly hosting environment, they are not a replacement for normal Pascal global variables or threadvars.
8894
8995Restrictions:
9096
9197- You cannot take the address of a wasmglobal.
9298- You cannot pass them as *var*, *constref* or *out* parameters.
9399- You cannot declare them in the local variables section of a procedure/function.
94100- *ABSOLUTE* is not supported, neither as a wasmglobal source, nor as a wasmglobal destination.
95101- TODO: initial value restrictions???
96102
97103---
98104*From the Free Pascal wiki, page "WebAssembly/Globals", as it stood on 2022-07-24 after a change by Nickysn, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
99105Category: WebAssembly

Back to history