Sign in Join
Wiki copytemporary

WebAssembly/Globals

WebAssembly 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.

Differences between Pascal global variables and WebAssembly globals

WebAssembly globals have the following restrictions:

These characteristics make them somewhat comparable to CPU or FPU registers on other architectures. However, they differ from registers in the following way:

In 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

Special WebAssembly globals

Free Pascal already creates and uses some WebAssembly globals, which are considered special and are part of the Basic C ABI, which Free Pascal supports.

__stack_pointer

WebAssembly 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.

TLS-related globals

When multithreading is enabled, the linker creates the following globals:

Pascal syntax proposal

WebAssembly globals are not yet implemented in Free Pascal. This section discusses the syntax for a possible language extension for supporting them.

pascal
var
  a: LongInt; wasmglobal;  // FPC's name mangling applies
  aa: LongInt = 5; wasmglobal;  // initialized global
  b: LongInt; wasmglobal name 'x';  // FPC's name mangling is not applied
  c: LongInt; wasmglobal; external name 'q';  // external, declared in a different unit, or .o module
  d: LongInt; wasmglobal; import name 'ddd';  // an import for the final .wasm module

const
  ccc: LongInt = 10; wasmglobal;  // immutable global

TODO: can we achieve this without introducing a new keyword?

Introducing 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:

pascal
var
  a: Longint;
  wasmglobal: Boolean;

Proposed by Jonas:

pascal
var
  a: LongInt; section 'WebAssembly.Global';

Key here is the fact that the 'section' keyword already exists.

Supported types:

Unsupported types:

Some 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.

Restrictions:


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.

Revision 2, last changed by WikiArchive .

Edited on wiki.freepascal.org by 1 person, 2022

From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.

WhenWhoWhat changed
NickysnCreated page
Nickysn
NickysnDifferences between Pascal global variables and WebAssembly globals
NickysnDifferences between Pascal global variables and WebAssembly globals (minor)
Nickysn
NickysnTLS-related globals
NickysnTLS-related globals
Nickysn
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnSpecial WebAssembly globals: - Basic C ABI external link added
NickysnPascal syntax proposal
NickysnPascal syntax proposal
NickysnDifferences between Pascal global variables and WebAssembly globals (minor)
NickysnDifferences between Pascal global variables and WebAssembly globals
NickysnDifferences between Pascal global variables and WebAssembly globals (minor)
NickysnDifferences between Pascal global variables and WebAssembly globals
NickysnDifferences between Pascal global variables and WebAssembly globals
NickysnPascal syntax proposal
NickysnDifferences between Pascal global variables and WebAssembly globals
NickysnPascal syntax proposal: - added the Jonas proposal for using section 'global'
NickysnPascal syntax proposal: - let's change the section name to 'WebAssembly.Global' - more verbose, yet readable and less likely to clash with a real object section name that someone is using

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.