1b1994897Sopenharmony_ci# Assembly File Format Specification 2b1994897Sopenharmony_ci 3b1994897Sopenharmony_ci## Introduction 4b1994897Sopenharmony_ci 5b1994897Sopenharmony_ciThis document describes assembly file format for Panda platform. Assembly files are human-readable and human-writeable plain text files, they are supposed to be fed to the Panda assembler, a dedicated tool that translates them to binary files that can be executed by the Panda virtual machine. Please note that this document does not describe bytecode instructions supported by the Panda virtual machine, refer to the [Bytecode ISA Specification](isa/isa.yaml) instead. This document does not specify the binary format of executables supported by the Panda virtual machine, please refer to the [Binary Format Specification](file_format.md) instead. 6b1994897Sopenharmony_ci 7b1994897Sopenharmony_ci### Requirements 8b1994897Sopenharmony_ci 9b1994897Sopenharmony_ciPanda as a platform is multilingual and flexible by design: 10b1994897Sopenharmony_ci 11b1994897Sopenharmony_ci* Panda assembly should not "favor" by any means any existing programming language that is (or intended to be) supported by the platform. Instead, Panda assembly can be thought as a separate close-to-byte-code language with a minimal feature set. All language-specific "traits" that should be supported to generate valid executable binaries with respect to the higher-level semantics should be implemented via metadata annotations (see below). 12b1994897Sopenharmony_ci* Panda assembly should not focus on a certain programming paradigm. E.g. we should not enforce concepts of "class", "object", "method" at the assembly language level because we might support a language which does not implement classic OOP at all. 13b1994897Sopenharmony_ci* When Panda assembler generates a binary executable file, it is not expected to check for language semantics. This responsibility is delegate to "source to binaries" compilers and runtime. 14b1994897Sopenharmony_ci* Panda assembler should not impose any limitation of quantity and internal structure of source code files writtebn in Panda assembly language. Assembler should process as many input source code files as the developer specifies. 15b1994897Sopenharmony_ci* Panda assembler should not follow any implicit conventions about the name of the entry point. 16b1994897Sopenharmony_ci 17b1994897Sopenharmony_ci## Comments 18b1994897Sopenharmony_ci 19b1994897Sopenharmony_ciComments are marked with the `#` character. All characters that follow it (including the `#` character itself) are ignored. 20b1994897Sopenharmony_ci 21b1994897Sopenharmony_ci## Literals 22b1994897Sopenharmony_ci 23b1994897Sopenharmony_ci### Numeric Literals 24b1994897Sopenharmony_ci 25b1994897Sopenharmony_ciFollowing numeric literals are supported: 26b1994897Sopenharmony_ci 27b1994897Sopenharmony_ci* Signed/Unsigned decimal/hexadecimal/binary integers not larger than 64 bits. Hexadecimal literals are prefixed with `0x`. Binary literals are prefixed with `0b`. 28b1994897Sopenharmony_ci* Floating-point decimal/hexadecimal literals that can be represented with IEEE 754. Hexadecimal floating-point literals are prefixed with `0x`. They are first converted to a bit representation that corresponds to a hex, and then converted to a double using a bit_cast in accordance with the IEEE 754 standard. 29b1994897Sopenharmony_ci 30b1994897Sopenharmony_ci### String Literals 31b1994897Sopenharmony_ci 32b1994897Sopenharmony_ciString literal is a sequence of any characters enclosed in `"` characters. Non-printable characters and characters out of Latin-1 character set must be encoded with `mutf8` encoding. For example: `"文字范例"` string literal should be encoded as `"\xe6\x96\x87\xe5\xad\x97\xe8\x8c\x83\xe4\xbe\x8b"` 33b1994897Sopenharmony_ci 34b1994897Sopenharmony_ciThe following escape sequences can be used in string literals: 35b1994897Sopenharmony_ci 36b1994897Sopenharmony_ci - `\"` double quote, `\x22` 37b1994897Sopenharmony_ci - `\a` alert, `\x07` 38b1994897Sopenharmony_ci - `\b` backspace, `\x08` 39b1994897Sopenharmony_ci - `\f` form feed, `\x0c` 40b1994897Sopenharmony_ci - `\n` newline, `\x0a` 41b1994897Sopenharmony_ci - `\r` carriage return, `\x0d` 42b1994897Sopenharmony_ci - `\t` horizontal tab, `\x09` 43b1994897Sopenharmony_ci 44b1994897Sopenharmony_ci## Identifiers 45b1994897Sopenharmony_ci 46b1994897Sopenharmony_ci### Simple Identifiers 47b1994897Sopenharmony_ci 48b1994897Sopenharmony_ciA simple identifier is a sequence of ASCII characters. Allowed characters in the sequence are: 49b1994897Sopenharmony_ci 50b1994897Sopenharmony_ci* Letters from `a` to `z`. 51b1994897Sopenharmony_ci* Letters from `A` to `Z`. 52b1994897Sopenharmony_ci* Digits from `0` to `9`. 53b1994897Sopenharmony_ci* Following characters: `_`, `$`. 54b1994897Sopenharmony_ci 55b1994897Sopenharmony_ciFollowing constraints apply: 56b1994897Sopenharmony_ci 57b1994897Sopenharmony_ci* A valid identifier starts with any letter or with `_`. 58b1994897Sopenharmony_ci* All identifiers are case sensitive. 59b1994897Sopenharmony_ci 60b1994897Sopenharmony_ciSimple identifiers can be used for naming metadata annotations, primitive data types, aggregate data types, members of aggregate data types, functions and labels. 61b1994897Sopenharmony_ci 62b1994897Sopenharmony_ci### Prefixed Identifiers 63b1994897Sopenharmony_ci 64b1994897Sopenharmony_ciA prefixed identifier is a sequence of simple identifiers delimited by the `.` char without whitespaces. 65b1994897Sopenharmony_ci 66b1994897Sopenharmony_ciPrefixed identifiers can be used for naming metadata annotations, aggregate data types and functions. 67b1994897Sopenharmony_ci 68b1994897Sopenharmony_ci## Metadata Annotations 69b1994897Sopenharmony_ci 70b1994897Sopenharmony_ciAs stated above, current version of Panda assembly does not favor any language as the platform is designed to support many of them. To deal with language-specific metadata, annotations are used, defined as follows: 71b1994897Sopenharmony_ci 72b1994897Sopenharmony_ci``` 73b1994897Sopenharmony_ci<key1=value1, key2=value2, ...> 74b1994897Sopenharmony_ci``` 75b1994897Sopenharmony_ci 76b1994897Sopenharmony_ciValues are optional. In such case, only `key` is needed. 77b1994897Sopenharmony_ci 78b1994897Sopenharmony_ciFollowing constraints apply: 79b1994897Sopenharmony_ci 80b1994897Sopenharmony_ci* Each key is a valid indetifier. 81b1994897Sopenharmony_ci* All keys are unique within a single annotation list. 82b1994897Sopenharmony_ci* If present, a value is a valid identifier, with following exception: Values can start with a digit. 83b1994897Sopenharmony_ci 84b1994897Sopenharmony_ciIn all cases where annotations can be optionally used, `optional_annotation` marker is used in this document. 85b1994897Sopenharmony_ci 86b1994897Sopenharmony_ciThere are keys that indicate that a function must not have an implementation. The absence of these keys suggests otherwise. We shall call metadata containing such keys --- `lonely metadata`. 87b1994897Sopenharmony_ci 88b1994897Sopenharmony_ci### Function metadata annotations 89b1994897Sopenharmony_ci 90b1994897Sopenharmony_ci#### Standard metadata 91b1994897Sopenharmony_ci 92b1994897Sopenharmony_ciA definition of a function is assumed. 93b1994897Sopenharmony_ci 94b1994897Sopenharmony_ci| Key | Description | 95b1994897Sopenharmony_ci| ------ | ------ | 96b1994897Sopenharmony_ci 97b1994897Sopenharmony_ci#### Lonely metadata 98b1994897Sopenharmony_ci 99b1994897Sopenharmony_ciA declaration of a function is assumed. 100b1994897Sopenharmony_ci 101b1994897Sopenharmony_ci| Key | Description | 102b1994897Sopenharmony_ci| ------ | ------ | 103b1994897Sopenharmony_ci| `external` | Marks an externally defined function. Does not require value. | 104b1994897Sopenharmony_ci| `native` | Marks an externally defined function. Does not require value. | 105b1994897Sopenharmony_ci| `noimpl` | Marks a function without implementation. Does not require value. | 106b1994897Sopenharmony_ci| `static` | Marks a function as static. Does not require value. | 107b1994897Sopenharmony_ci| `ctor` | Marks a function as object constructor. It will be renamed in binary file according to particular language rules (`.ctor` for Panda Assembly) | 108b1994897Sopenharmony_ci| `cctor` | Marks a function as static constructor. It will be renamed in binary file according to particular language rules (`.cctor` for Panda Assembly) | 109b1994897Sopenharmony_ci 110b1994897Sopenharmony_ci### Record metadata annotations 111b1994897Sopenharmony_ci 112b1994897Sopenharmony_ci#### Standard metadata 113b1994897Sopenharmony_ci 114b1994897Sopenharmony_ciA definition of a record is assumed. 115b1994897Sopenharmony_ci 116b1994897Sopenharmony_ci| Key | Description | 117b1994897Sopenharmony_ci| ------ | ------ | 118b1994897Sopenharmony_ci 119b1994897Sopenharmony_ci#### Lonely metadata 120b1994897Sopenharmony_ci 121b1994897Sopenharmony_ciA declaration of a record is assumed. 122b1994897Sopenharmony_ci 123b1994897Sopenharmony_ci| Key | Description | 124b1994897Sopenharmony_ci| ------ | ------ | 125b1994897Sopenharmony_ci| `external` | Marks an externally defined record. Does not require value. | 126b1994897Sopenharmony_ci 127b1994897Sopenharmony_ci### Field metadata annotations 128b1994897Sopenharmony_ci 129b1994897Sopenharmony_ci| Key | Description | 130b1994897Sopenharmony_ci| ------ | ------ | 131b1994897Sopenharmony_ci| `external` | Marks an externally defined field. Does not require value. | 132b1994897Sopenharmony_ci| `static` | Marks an statically defined field. Does not require value. | 133b1994897Sopenharmony_ci 134b1994897Sopenharmony_ci### Language specific annotations 135b1994897Sopenharmony_ci 136b1994897Sopenharmony_ciCurrently Panda Assembly supports annotations for the following languages: 137b1994897Sopenharmony_ci 138b1994897Sopenharmony_ci- Java 139b1994897Sopenharmony_ci- PandaAssembly 140b1994897Sopenharmony_ci 141b1994897Sopenharmony_ciTo specify language `.language` directive is used. It must be declared before any other declarations: 142b1994897Sopenharmony_ci``` 143b1994897Sopenharmony_ci.language Java 144b1994897Sopenharmony_ci 145b1994897Sopenharmony_ci.function void f() {} 146b1994897Sopenharmony_ci``` 147b1994897Sopenharmony_ciBy default PandaAssembly language is assumed. 148b1994897Sopenharmony_ci 149b1994897Sopenharmony_ci#### Java annotations 150b1994897Sopenharmony_ci 151b1994897Sopenharmony_ciCurrently Panda Assembly supports following Java annotations 152b1994897Sopenharmony_ci 153b1994897Sopenharmony_ci| Key | Description | 154b1994897Sopenharmony_ci| --- | --- | 155b1994897Sopenharmony_ci| `java.access` | Used to specify access level of record, field or function. Possible values: `private`, `protected`, `public`. | 156b1994897Sopenharmony_ci| `java.extends` | Used to specify inheritance between records. Value is the name of the base record. | 157b1994897Sopenharmony_ci| `java.implements` | Used to specify interface inheritance between records. Value is the name of the interface record. Allowed multiple definition. | 158b1994897Sopenharmony_ci| `java.interface` | Used to specify that the record represents Java interface. | 159b1994897Sopenharmony_ci| `java.enum` | Used to specify that the record and its fields represent Java enum. | 160b1994897Sopenharmony_ci| `java.annotation` | Used to specify that the record represents Java annotation. | 161b1994897Sopenharmony_ci| `java.annotation.type` | Used to specify type of annotation. Possible values: `class`, `runtime`. | 162b1994897Sopenharmony_ci| `java.annotation.class` | Used to specify annotation class. Allowed multiple definitions. Value is the name of the record that represent Java annotation | 163b1994897Sopenharmony_ci| `java.annotation.id` | Used to specify annotation id. Annotations with id are used as values of other annotation elements. `java.annotation.class` must be defined first. Allowed multiple definitions (but only one definition for each annotation). | 164b1994897Sopenharmony_ci| `java.annotation.element.name` | Used to specify name of the annotation element. `java.annotation.class` must be defined first. Allowed multiple definitions (but only one definition for each annotation element). | 165b1994897Sopenharmony_ci| `java.annotation.element.type` | Used to specify type of the annotation element. `java.annotation.element.name` must be defined first. Allowed multiple definitions (but only one definition for each annotation element). Possible values: `u1`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `f32`, `f64`, `string`, `class`, `enum`, `annotation`, `array`. | 166b1994897Sopenharmony_ci| `java.annotation.element.array.component.type` | Used to specify component type of the array annotation element. `java.annotation.element.type` must be defined first and have `array` value. Allowed multiple definitions (but only one definition for each annotation element). Possible values: `u1`, `i8`, `u8`, `i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `f32`, `f64`, `string`, `class`, `enum`, `annotation`. | 167b1994897Sopenharmony_ci| `java.annotation.element.value` | Used to specify value of the annotation element. Allowed multiple definitions (also multiple definitions for one annotation element if it has `array` type). | 168b1994897Sopenharmony_ci 169b1994897Sopenharmony_ci 170b1994897Sopenharmony_ciExample: 171b1994897Sopenharmony_ci 172b1994897Sopenharmony_ci``` 173b1994897Sopenharmony_ci.language Java 174b1994897Sopenharmony_ci 175b1994897Sopenharmony_ci.record A <java.access=public> {} 176b1994897Sopenharmony_ci.record B <java.access=public, java.extends=A> {} 177b1994897Sopenharmony_ci 178b1994897Sopenharmony_ci.record Iface1 <java.interface> 179b1994897Sopenharmony_ci.record Iface2 <java.interface> 180b1994897Sopenharmony_ci 181b1994897Sopenharmony_ci.record C <java.implements=Iface1, java.implements=Iface2> {} 182b1994897Sopenharmony_ci 183b1994897Sopenharmony_ci.record A1 <java.annotation, java.annotation.type=runtime> {} 184b1994897Sopenharmony_ci.record A2 <java.annotation, java.annotation.type=runtime> {} 185b1994897Sopenharmony_ci 186b1994897Sopenharmony_ci# Annotation elements are represented using abstract methods 187b1994897Sopenharmony_ci 188b1994897Sopenharmony_ci.function i32[] A1.NameArr() <noimpl> 189b1994897Sopenharmony_ci.function A1 A2.Name() <noimpl> 190b1994897Sopenharmony_ci 191b1994897Sopenharmony_ci# @A2(Name=@A1(NameArr={1,2})) 192b1994897Sopenharmony_ci.record R <java.annotation.class=A1, java.annotation.id=id1, java.annotation.element.name=NameArr, java.annotation.element.type=array, java.annotation.element.array.component.type=i32, java.annotation.element.value=1, java.annotation.element.value=2, java.annotation.class=A2, java.annotation.element.name=Name, java.annotation.element.type=annotation, java.annotation.element.value=id1> 193b1994897Sopenharmony_ci``` 194b1994897Sopenharmony_ci 195b1994897Sopenharmony_ci## Data Types 196b1994897Sopenharmony_ci 197b1994897Sopenharmony_ciSemantics of operations on all data types defined below follows the semantics defined in [Bytecode ISA Specification](isa/isa.yaml). 198b1994897Sopenharmony_ci 199b1994897Sopenharmony_ci### Primitive Data Types 200b1994897Sopenharmony_ci 201b1994897Sopenharmony_ciFollowing primitive types are supported: 202b1994897Sopenharmony_ci 203b1994897Sopenharmony_ci| Panda Assembler Type | Description | 204b1994897Sopenharmony_ci| ------ | ------ | 205b1994897Sopenharmony_ci| `void` | Type for the result of a function that returns normally, but does not provide a result value to its caller | 206b1994897Sopenharmony_ci| `u1` | Unsinged 1-bit integer number | 207b1994897Sopenharmony_ci| `u8` | Unsigned 8-bit integer number | 208b1994897Sopenharmony_ci| `i8` | Signed 8-bit integer number | 209b1994897Sopenharmony_ci| `u16` | Unsigned 16-bit integer number | 210b1994897Sopenharmony_ci| `i16` | Signed 16-bit integer number | 211b1994897Sopenharmony_ci| `u32` | Unsigned 32-bit integer number | 212b1994897Sopenharmony_ci| `i32` | Signed 32-bit integer number | 213b1994897Sopenharmony_ci| `u64` | Unsigned 64-bit integer number | 214b1994897Sopenharmony_ci| `i64` | Signed 64-bit integer number | 215b1994897Sopenharmony_ci| `f32` | 32-bit single precision floating point number, compliant with IEEE 754 standard | 216b1994897Sopenharmony_ci| `f64` | 64-bit double precision floating point number, compliant with IEEE 754 standard | 217b1994897Sopenharmony_ci 218b1994897Sopenharmony_ciAll identifiers that are used for naming primitive data types cannot be used for any other purpose. 219b1994897Sopenharmony_ci 220b1994897Sopenharmony_ci### Reference Data Types 221b1994897Sopenharmony_ci 222b1994897Sopenharmony_ciFollowing reference types are supported: 223b1994897Sopenharmony_ci 224b1994897Sopenharmony_ci| Panda Assembler Type | Description | 225b1994897Sopenharmony_ci| ------ | ------ | 226b1994897Sopenharmony_ci| `cref` | code reference, represents references to the bytecode executable by Panda virtual machine | 227b1994897Sopenharmony_ci| `dref` | data reference, represents references to aggregate data types (see below) | 228b1994897Sopenharmony_ci 229b1994897Sopenharmony_ciAll identifiers that are used for naming reference data types cannot be used for any other purpose. 230b1994897Sopenharmony_ci 231b1994897Sopenharmony_ci### Aggregate Data Types 232b1994897Sopenharmony_ci 233b1994897Sopenharmony_ciAggregate data types are defined as follows: 234b1994897Sopenharmony_ci 235b1994897Sopenharmony_ci``` 236b1994897Sopenharmony_ci.record RecordName optional_annotation { 237b1994897Sopenharmony_ci type1 member1 optional_annotation1 238b1994897Sopenharmony_ci type2 member2 optional_annotation2 239b1994897Sopenharmony_ci # ... 240b1994897Sopenharmony_ci typeN memberN optional_annotationN 241b1994897Sopenharmony_ci} 242b1994897Sopenharmony_ci``` 243b1994897Sopenharmony_ci 244b1994897Sopenharmony_ciFollowing constraints apply: 245b1994897Sopenharmony_ci 246b1994897Sopenharmony_ci* `RecordName`, `type1`, ... `typeN`, `member1`, ... `memberN` are valid identifiers. 247b1994897Sopenharmony_ci* `member1`, ... `memberN` are unique identifiers within a record. 248b1994897Sopenharmony_ci* `RecordName` is unique across all source code files. 249b1994897Sopenharmony_ci 250b1994897Sopenharmony_ciWhenever a record should incorporate another record, the name of the nested record must be specified. However, in this context this name implicitly denotes a `dref` type which implements a reference to the data represented by that record. Example: 251b1994897Sopenharmony_ci 252b1994897Sopenharmony_ci``` 253b1994897Sopenharmony_ci.record Foo { 254b1994897Sopenharmony_ci i32 member1 255b1994897Sopenharmony_ci f32 member2 256b1994897Sopenharmony_ci} 257b1994897Sopenharmony_ci 258b1994897Sopenharmony_ci.record Bar { 259b1994897Sopenharmony_ci Foo foo 260b1994897Sopenharmony_ci f64 member1 261b1994897Sopenharmony_ci f64 member2 262b1994897Sopenharmony_ci} 263b1994897Sopenharmony_ci``` 264b1994897Sopenharmony_ci 265b1994897Sopenharmony_ci#### Informal Notice 266b1994897Sopenharmony_ci 267b1994897Sopenharmony_ci`.record`s are like `struct`s in C, but without support for "by instance" nesting. This is because the result of a field load should be valid for any member, hence a record member should fit the virtusal register. Constraints on register are defined in [Bytecode ISA Specification](isa/isa.yaml). 268b1994897Sopenharmony_ci 269b1994897Sopenharmony_ci### Builtin Aggregate Data Types 270b1994897Sopenharmony_ci 271b1994897Sopenharmony_ciPlatform has following builtin aggregate types 272b1994897Sopenharmony_ci 273b1994897Sopenharmony_ci| Panda Assembler Type | Description | 274b1994897Sopenharmony_ci| ------ | ------ | 275b1994897Sopenharmony_ci| `panda.String` | UTF16 string | 276b1994897Sopenharmony_ci 277b1994897Sopenharmony_ci### Arrays 278b1994897Sopenharmony_ci 279b1994897Sopenharmony_ciPlatform support arrays of primitive and aggregate data types. Array of type `T` has type name `T[]`. Example: 280b1994897Sopenharmony_ci``` 281b1994897Sopenharmony_ci.function void f() { 282b1994897Sopenharmony_ci ... 283b1994897Sopenharmony_ci newarr v1, v0, i32[] 284b1994897Sopenharmony_ci ... 285b1994897Sopenharmony_ci newarr v1, v0, panda.String[] 286b1994897Sopenharmony_ci ... 287b1994897Sopenharmony_ci newarr v1, v0, f32[][][] 288b1994897Sopenharmony_ci ... 289b1994897Sopenharmony_ci} 290b1994897Sopenharmony_ci``` 291b1994897Sopenharmony_ci 292b1994897Sopenharmony_ci## Functions 293b1994897Sopenharmony_ci 294b1994897Sopenharmony_ciFunctions are defined as follows: 295b1994897Sopenharmony_ci 296b1994897Sopenharmony_ci``` 297b1994897Sopenharmony_ci.function FunctionName(ArgumentType0 a0, ... ArgumentTypeN argN) optional_annotation 298b1994897Sopenharmony_ci{ 299b1994897Sopenharmony_ci # code 300b1994897Sopenharmony_ci} 301b1994897Sopenharmony_ci``` 302b1994897Sopenharmony_ci 303b1994897Sopenharmony_ciFollowing constraints apply: 304b1994897Sopenharmony_ci 305b1994897Sopenharmony_ci* `FunctionName`, `ArgumentType0`, ... `ArgumentTypeN`, `a0`, ... `aN` are valid identifiers. 306b1994897Sopenharmony_ci* All `a0`, ... `aN` are unique within the argument list of the function. 307b1994897Sopenharmony_ci* `FunctionName` is unique across all source code files. 308b1994897Sopenharmony_ci 309b1994897Sopenharmony_ci### Function Arguments and Local Variables 310b1994897Sopenharmony_ci 311b1994897Sopenharmony_ciBy convention, all arguments are named `a0`, ... `aN` and all local variables are named `v0`, ... `vM`. Panda assembler guarantees that all these entities are unambiguously mapped to the underlying virtual registers. 312b1994897Sopenharmony_ci 313b1994897Sopenharmony_ci### Function Body 314b1994897Sopenharmony_ci 315b1994897Sopenharmony_ciIf a function has a body, it consists of optionally labeled sequence of bytecode instructions, one instruction defined per line. Instruction opcodes and formats follow [Bytecode ISA Specification](isa/isa.yaml). 316b1994897Sopenharmony_ci 317b1994897Sopenharmony_ci### Static and virtual functions 318b1994897Sopenharmony_ci 319b1994897Sopenharmony_ciBy default all function are static except ones that are binded to record and accept reference to it as the first parameter: 320b1994897Sopenharmony_ci 321b1994897Sopenharmony_ci``` 322b1994897Sopenharmony_ci.record R {} 323b1994897Sopenharmony_ci 324b1994897Sopenharmony_ci.function void R.foo(R a0) {} # virtual function 325b1994897Sopenharmony_ci 326b1994897Sopenharmony_ci.function void R.foo(R a0) <static> {} # static function 327b1994897Sopenharmony_ci 328b1994897Sopenharmony_ci.function void R.foo(i32 a0) {} # static function 329b1994897Sopenharmony_ci``` 330b1994897Sopenharmony_ci 331b1994897Sopenharmony_ci#### Call instructions 332b1994897Sopenharmony_ci 333b1994897Sopenharmony_ciAssembler relaxes constraints for call instructions: 334b1994897Sopenharmony_ci 335b1994897Sopenharmony_ci- If number of arguments is less than specified in [Bytecode ISA Specification](isa/isa.yaml) it passes `v0` instead of unspecified ones. 336b1994897Sopenharmony_ci 337b1994897Sopenharmony_ci- For non range call instructions assembler chooses optimal encoding according to number of specified arguments. 338b1994897Sopenharmony_ci 339b1994897Sopenharmony_ciExample: 340b1994897Sopenharmony_ci 341b1994897Sopenharmony_ciFollowing instruction in assembly 342b1994897Sopenharmony_ci``` 343b1994897Sopenharmony_cicall.static f, v1 344b1994897Sopenharmony_ci``` 345b1994897Sopenharmony_ciwill be emitted as 346b1994897Sopenharmony_ci``` 347b1994897Sopenharmony_cicall.short.static f, v1, v0 348b1994897Sopenharmony_ci``` 349b1994897Sopenharmony_ci 350b1994897Sopenharmony_ci### Program Entry Point 351b1994897Sopenharmony_ci 352b1994897Sopenharmony_ciAny function which accepts an array of strings as its single argument may serve as a program entry point. The name of the entry point must be specified as a part of the input to the assembler program. An example of a possible entry point is: 353b1994897Sopenharmony_ci 354b1994897Sopenharmony_ci``` 355b1994897Sopenharmony_ci.record _panda_array_string <external> 356b1994897Sopenharmony_ci 357b1994897Sopenharmony_ci.function foo(_panda_array_string a0) 358b1994897Sopenharmony_ci{ 359b1994897Sopenharmony_ci # code 360b1994897Sopenharmony_ci} 361b1994897Sopenharmony_ci``` 362b1994897Sopenharmony_ci 363b1994897Sopenharmony_ci### Exception handlers 364b1994897Sopenharmony_ci 365b1994897Sopenharmony_ciTry, catch and finally blocks can be declared using `.catch` and `.catchall` directives: 366b1994897Sopenharmony_ci``` 367b1994897Sopenharmony_ci.catch <exception_record>, <try_begin_label>, <try_end_label>, <catch_begin_label> 368b1994897Sopenharmony_ci.catchall <try_begin_label>, <try_end_label>, <catch_begin_label> 369b1994897Sopenharmony_ci``` 370b1994897Sopenharmony_ci 371b1994897Sopenharmony_ciExample: 372b1994897Sopenharmony_ci``` 373b1994897Sopenharmony_ci.record Exception1 {} 374b1994897Sopenharmony_ci.record Exception2 {} 375b1994897Sopenharmony_ci 376b1994897Sopenharmony_ci.function void foo() 377b1994897Sopenharmony_ci{ 378b1994897Sopenharmony_ci ... 379b1994897Sopenharmony_citry_begin: 380b1994897Sopenharmony_ci ... 381b1994897Sopenharmony_citry_end: 382b1994897Sopenharmony_ci ... 383b1994897Sopenharmony_cicatch_begin1: 384b1994897Sopenharmony_ci ... 385b1994897Sopenharmony_cicatch_begin2: 386b1994897Sopenharmony_ci ... 387b1994897Sopenharmony_cicatchall_begin1: 388b1994897Sopenharmony_ci ... 389b1994897Sopenharmony_ci 390b1994897Sopenharmony_ci .catch Exception1, try_begin, try_end, catch_begin1 391b1994897Sopenharmony_ci .catch Exception2, try_begin, try_end, catch_begin2 392b1994897Sopenharmony_ci .catchall try_begin, try_end, catchall_begin1 393b1994897Sopenharmony_ci} 394b1994897Sopenharmony_ci``` 395b1994897Sopenharmony_ci 396b1994897Sopenharmony_ciAlso there are more safer directives, which allow to specify exact bounds 397b1994897Sopenharmony_ciof an exceptions handler for more precise verification of control-flow in 398b1994897Sopenharmony_cibyte-code verifier. 399b1994897Sopenharmony_ci 400b1994897Sopenharmony_ci``` 401b1994897Sopenharmony_ci.catch <exception_record>, <try_begin_label>, <try_end_label>, <catch_begin_label>, <catch_end_label> 402b1994897Sopenharmony_ci.catchall <try_begin_label>, <try_end_label>, <catch_begin_label>, <catch_end_label> 403b1994897Sopenharmony_ci``` 404b1994897Sopenharmony_ci 405b1994897Sopenharmony_ciThey are almost identical to `.catch` and `.catchall` differ only by specifying end label of the 406b1994897Sopenharmony_ciexception handler. End label is the label that immediately follows last instruction of the 407b1994897Sopenharmony_ciexception handler. 408b1994897Sopenharmony_ci 409b1994897Sopenharmony_ci## Pseudo-BNF 410b1994897Sopenharmony_ci 411b1994897Sopenharmony_ciInstruction flow is omitted for simplicity: 412b1994897Sopenharmony_ci 413b1994897Sopenharmony_ci``` 414b1994897Sopenharmony_ci# Literals are represented in double-quotes as "literal value". 415b1994897Sopenharmony_ci# Free-form descriptions are represented as "<description here>" 416b1994897Sopenharmony_ci# Empty symbol is represented as E. 417b1994897Sopenharmony_ci 418b1994897Sopenharmony_cidefs := defs def | E 419b1994897Sopenharmony_cidef := rec_def | func_def 420b1994897Sopenharmony_ci 421b1994897Sopenharmony_ci# Identifiers: 422b1994897Sopenharmony_ci 423b1994897Sopenharmony_ciletter_lower := "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" 424b1994897Sopenharmony_ciletter_upper := "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" 425b1994897Sopenharmony_cidigit := "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" 426b1994897Sopenharmony_cichar_misc := "_" 427b1994897Sopenharmony_ci 428b1994897Sopenharmony_cichar_non_dig := letter_lower | letter_upper | char_misc 429b1994897Sopenharmony_cichar_simple := char_non_dig | digit 430b1994897Sopenharmony_ci 431b1994897Sopenharmony_ciid_simple := char_non_dig id_simple_tail 432b1994897Sopenharmony_ciid_simple_tail := id_simple_tail char_simple | E 433b1994897Sopenharmony_ci 434b1994897Sopenharmony_ciid_prefixed := id_simple | id_simple "." id_prefixed 435b1994897Sopenharmony_ci 436b1994897Sopenharmony_ci# Records and types: 437b1994897Sopenharmony_cirec_def := ".record" rec_name rec_add 438b1994897Sopenharmony_cirec_add := def_pair_rec_meta rec_body | def_lonely_rec_meta 439b1994897Sopenharmony_cirec_name := id_prefixed 440b1994897Sopenharmony_cirec_body := "{" fields "}" 441b1994897Sopenharmony_citype_def := "u1" | "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "i64" | "f32" | "f64" | "any" | rec_name | type_def [] 442b1994897Sopenharmony_ci 443b1994897Sopenharmony_ci# Fields of records: 444b1994897Sopenharmony_cifields := fields field_def | E 445b1994897Sopenharmony_cifield_def := field_type field_name def_field_meta 446b1994897Sopenharmony_cifield_type := type_def 447b1994897Sopenharmony_cifield_name := id_simple 448b1994897Sopenharmony_ci 449b1994897Sopenharmony_ci# Functions: 450b1994897Sopenharmony_cifunc_def := ".function" func_sig func_add 451b1994897Sopenharmony_cifunc_add := def_pair_func_meta func_body | def_lonely_func_meta 452b1994897Sopenharmony_cifunc_sig := func_ret func_name func_args 453b1994897Sopenharmony_cifunc_ret := type_def 454b1994897Sopenharmony_cifunc_name := id_prefixed 455b1994897Sopenharmony_cifunc_args := "(" arg_list ")" 456b1994897Sopenharmony_ciarg_list := <","-separated list of argument names and their respective types> 457b1994897Sopenharmony_cifunc_body := "{" func_code "}" 458b1994897Sopenharmony_cifunc_code := <newline-separated sequence of bytecode instructions and their operands> 459b1994897Sopenharmony_ci 460b1994897Sopenharmony_ci# Function metadata annotations: 461b1994897Sopenharmony_cidef_pair_func_meta := "<" func_meta_list ">" | E 462b1994897Sopenharmony_cidef_lonely_func_meta := "<" func_lonely_meta_list ">" 463b1994897Sopenharmony_cifunc_meta_list := func_meta_list func_meta_item "," | E 464b1994897Sopenharmony_cifunc_meta_item := func_kv_pair | func_id 465b1994897Sopenharmony_cifunc_kv_pair := <an element of the function standard metadata list that assumes the assignment of a value, and the value that is assigned to it, separated by the sign "="> 466b1994897Sopenharmony_cifunc_id := <an element of the function standard metadata list> 467b1994897Sopenharmony_cifunc_lonely_meta_list := func_lonely_meta_list func_meta_item "," | E 468b1994897Sopenharmony_cifunc_meta_item := func_kv_lonely_pair | func_lonely_id 469b1994897Sopenharmony_cifunc_kv_lonely_pair := <an element of the function lonely metadata list that assumes the assignment of a value, and the value that is assigned to it, separated by the sign "="> 470b1994897Sopenharmony_cifunc_lonely_id := <an element of the function lonely metadata list> 471b1994897Sopenharmony_ci 472b1994897Sopenharmony_ci# Record metadata annotations: 473b1994897Sopenharmony_cidef_pair_rec_meta := "<" rec_meta_list ">" | E 474b1994897Sopenharmony_cidef_lonely_rec_meta := "<" rec_lonely_meta_list ">" 475b1994897Sopenharmony_cirec_meta_list := rec_meta_list rec_meta_item "," | E 476b1994897Sopenharmony_cirec_meta_item := rec_kv_pair | rec_id 477b1994897Sopenharmony_cirec_kv_pair := <an element of the record standard metadata list that assumes the assignment of a value, and the value that is assigned to it, separated by the sign "="> 478b1994897Sopenharmony_cirec_id := <an element of the record standard metadata list> 479b1994897Sopenharmony_cirec_lonely_meta_list := rec_lonely_meta_list rec_meta_item "," | E 480b1994897Sopenharmony_cirec_meta_item := rec_kv_lonely_pair | rec_lonely_id 481b1994897Sopenharmony_cirec_kv_lonely_pair := <an element of the record lonely metadata list that assumes the assignment of a value, and the value that is assigned to it, separated by the sign "="> 482b1994897Sopenharmony_cirec_lonely_id := <an element of the record lonely metadata list> 483b1994897Sopenharmony_ci 484b1994897Sopenharmony_ci# Field metadata annotations: 485b1994897Sopenharmony_cidef_field_meta := "<" field_meta_list ">" | E 486b1994897Sopenharmony_cifield_meta_list := field_meta_list field_meta_item "," | E 487b1994897Sopenharmony_cifield_meta_item := field_kv_pair | field_id 488b1994897Sopenharmony_cifield_kv_pair := <an element of the field metadata list that assumes the assignment of a value, and the value that is assigned to it, separated by the sign "="> 489b1994897Sopenharmony_cifield_id := <an element of the field metadata list> 490b1994897Sopenharmony_ci``` 491b1994897Sopenharmony_ci 492b1994897Sopenharmony_ci## Important notes 493b1994897Sopenharmony_ci 494b1994897Sopenharmony_ci- Assembler doesn't guarantee that functions, records and their fields will be located in binary file in the same order as they are located in assembly one 495b1994897Sopenharmony_ci 496b1994897Sopenharmony_ci## Appendix A, Informative: Code Layout Sample 497b1994897Sopenharmony_ci 498b1994897Sopenharmony_ci``` 499b1994897Sopenharmony_ci# External records and functions: 500b1994897Sopenharmony_ci.record Record1 <external> 501b1994897Sopenharmony_ci.function Record1.function1(Record1 a0, f64 a1) <external> 502b1994897Sopenharmony_ci 503b1994897Sopenharmony_ci.record Foo <java.extends=SomeRecord> { 504b1994897Sopenharmony_ci i32 member1 <java.access=private> 505b1994897Sopenharmony_ci i32 member2 <java.access=public> 506b1994897Sopenharmony_ci i32 member3 <java.access=static, java.instantiation=static> 507b1994897Sopenharmony_ci} 508b1994897Sopenharmony_ci 509b1994897Sopenharmony_ci.function Foo.constructor1(Foo a0) <java.ctor> 510b1994897Sopenharmony_ci{ 511b1994897Sopenharmony_ci # code for an overloaded "constructor" (whatever you mean by it) 512b1994897Sopenharmony_ci} 513b1994897Sopenharmony_ci 514b1994897Sopenharmony_ci.function Foo.constructor2(Foo a0, i32 a1) <java.ctor> 515b1994897Sopenharmony_ci{ 516b1994897Sopenharmony_ci # code for an overloaded "constructor" (whatever you mean by it) 517b1994897Sopenharmony_ci} 518b1994897Sopenharmony_ci 519b1994897Sopenharmony_ci.function Foo.func1(Foo a0, i32 a1) <java.access=public> 520b1994897Sopenharmony_ci{ 521b1994897Sopenharmony_ci # code 522b1994897Sopenharmony_ci} 523b1994897Sopenharmony_ci 524b1994897Sopenharmony_ci# "Interface" function: 525b1994897Sopenharmony_ci.function Foo.func2(Foo a0, i32 a1) <noimpl> 526b1994897Sopenharmony_ci 527b1994897Sopenharmony_ci.function entry_point(_panda_array_string a0) 528b1994897Sopenharmony_ci{ 529b1994897Sopenharmony_ci # After loading the binary, control will be transferred here 530b1994897Sopenharmony_ci} 531b1994897Sopenharmony_ci``` 532b1994897Sopenharmony_ci 533b1994897Sopenharmony_ciApart from metadata annotations, `Foo.` prefixes (remaining a pure naming convention for the assembler!) can be additionally processed during linkage to "bind" functions to records making them "true" methods from the OOP world. 534b1994897Sopenharmony_ci 535b1994897Sopenharmony_ci**Strings** and **arrays** can be thought as `external` record with some manipulating functions. There is no support for generics due to the low-level nature of the assembler, hence arrays of different types are implemented with different external record. 536b1994897Sopenharmony_ci 537b1994897Sopenharmony_ci## Appendix B, Informative: Mapping Panda Assembler TYpes to JVM Types 538b1994897Sopenharmony_ci 539b1994897Sopenharmony_ciThis section serves purely illustrative purposes. 540b1994897Sopenharmony_ci 541b1994897Sopenharmony_ci| Panda Assembler Type | Corresponding JVM Type | 542b1994897Sopenharmony_ci| ------ | ------ | 543b1994897Sopenharmony_ci| `u1` | `bool` | 544b1994897Sopenharmony_ci| `u8` | N/A | 545b1994897Sopenharmony_ci| `i8` | `byte` | 546b1994897Sopenharmony_ci| `u16` | `char` | 547b1994897Sopenharmony_ci| `i16` | `short` | 548b1994897Sopenharmony_ci| `u32` | N/A | 549b1994897Sopenharmony_ci| `i32` | `int` | 550b1994897Sopenharmony_ci| `u64` | N/A | 551b1994897Sopenharmony_ci| `i64` | `long` | 552b1994897Sopenharmony_ci| `f32` | `float` | 553b1994897Sopenharmony_ci| `f64` | `double` | 554b1994897Sopenharmony_ci| `cref` | N/A | 555b1994897Sopenharmony_ci| `dref` | `reference` | 556b1994897Sopenharmony_ci 557b1994897Sopenharmony_ci## Appendix C, TODO List 558b1994897Sopenharmony_ci 559b1994897Sopenharmony_ci* Specify `cref` and indirect calls to functions. 560b1994897Sopenharmony_ci* Elaborate on bytecode definition. 561b1994897Sopenharmony_ci* Compose formal definitions for literals. 562