WebAssembly/Globals

From Free Pascal wiki
Jump to navigationJump to search

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:

  • They can be only one of the 4 WebAssembly basic types - int32, int64, float32 or float64.
  • Their address cannot be taken. They don't exist anywhere in linear memory.
  • In a multithreaded environment, they are usually not shared between threads, i.e. they behave like threadvars.

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

  • There is an unlimited number of them.
  • They can be imported, exported, or just declared in a module (i.e. neither imported, nor exported, just declared and used by the module).

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:

  • __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.
  • __tls_align - the alignment requirement for the thread local block.
  • __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.

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.

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:

var
  a: Longint;
  wasmglobal: Boolean;

Proposed by Jonas:

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

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

Supported types:

  • LongInt (32-bit signed int) - mapped to i32
  • LongWord (32-bit unsigned int) - mapped to i32
  • Int64 (64-bit signed int) - mapped to i64
  • QWord (64-bit unsigned int) - mapped to i64
  • Single - mapped to f32
  • Double - mapped to f64
  • Pointer (typed or untyped) - mapped to i32

Unsupported types:

  • enum
  • set
  • string
  • class
  • object
  • record
  • smallint/shortint/byte/word
  • char
  • boolean
  • array
  • variant

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:

  • You cannot take the address of a wasmglobal.
  • You cannot pass them as var, constref or out parameters.
  • You cannot declare them in the local variables section of a procedure/function.
  • ABSOLUTE is not supported, neither as a wasmglobal source, nor as a wasmglobal destination.
  • TODO: initial value restrictions???