Keywords: pcode

Up: HAFTA Overlord P-Code Definition Previous: 4 Header Codes Contents
Subsections

- 5.1 Named Opcodes
  - 5.1.1 call
  - 5.1.2 alarm
  - 5.1.3 signal
  - 5.1.4 sigchild
  - 5.1.5 get
  - 5.1.6 set
  - 5.1.7 getpid
  - 5.1.8 setpid
  - 5.1.9 return
  - 5.1.10 inst
  - 5.1.11 dinst
  - 5.1.12 drop
  - 5.1.13 dup
- 5.2 Unnamed Opcodes
  - 5.2.1 context
- 5.3 Symboled Opcodes
  - 5.3.1 IF / FI
  - 5.3.2 FOR / ROF
  - 5.3.3 Binary Logical Opcodes
  - 5.3.4 Binary Arithmetical Opcodes
  - 5.3.5 Unary Opcodes
- 5.4 Reference Constructs
  - 5.4.1 mark
  - 5.4.2 reference
  - 5.4.3 context
  - 5.4.4 module
- 5.5 Constant constructs
  - 5.5.1 String construct
  - 5.5.2 Integer construct

# 5 Opcodes

There are 13 named opcodes, 1 unnamed opcodes, 15 symboled opcodes, 4 reference constructs, a static string construct, and a static integer construct.

Comments appear in the unassembled pcode as a semicolon, followed by the comment, and terminated by an eol character.

## 5.1 Named Opcodes

Named opcodes appear in the unassembled p-code as keyword names, and appear in the assembled p-code as char sized unsigned integers.

The definitions of the named opcodes are as follows.

### 5.1.1 call

/ref call #retval

The call opcode invokes a p-code segment. Upon completion, the return value of the segment is pushed on the stack.

### 5.1.2 alarm

#delay /ref alarm

The alarm opcode invokes a p-code section after the specified delay.

### 5.1.3 signal

#sig /ref signal

The signal opcode invokes a p-code section upon the overlord receiving the specified signal.

Valid signals are #1 (SIGUSR1) and #2 (SIGUSR2).

Note: The handler is cleared once the signal is triggered.

### 5.1.4 sigchild

/ref sigchild

the sigchild opcode invokes a p-code section when the overlord receiving the sigchild signal and the waitpid() call returns a pid matching the pid in the current context.

Note: The handler is cleared once the signal is triggered.

### 5.1.5 get

args .. args (nebuloid) get returnval

The get opcode invokes a nebuloid's get function with the provided args. Once completed, the return value from the nebuloid is pushed onto the stack.

### 5.1.6 set

value (nebuloid) set

The set opcode invokes a nebuloid's set function with the provided value.

### 5.1.7 getpid

getpid #pid

The getpid opcode retrieves the pid associated with the current context and places it on the stack.

### 5.1.8 setpid

#pid setpid

The setpid opcode associates the provided pid with the current context.

### 5.1.9 return

#retval return

The return opcode stops execution of the current p-code segment, and returns to executing the calling segment if the segment was called from pcode, or waits for an alarm/signal to retrigger execution.

The #retval argument is pushed back onto the stack after it is trimmed if the segment was called from pcode, otherwise it is discarded.

### 5.1.10 inst

args .. args #local &module (nebuloid) inst

The inst opcode instantiates an instance of a nebuloid. It binds a module to a nebuloid name in either the global or local context (depending if the local arg evaluates as true or false), and passes the provided args to the nebuloid's instantiation routine.

### 5.1.11 dinst

(nebuloid) dinst

The dinst opcode de-instantiates an instance of a nebuloid.

### 5.1.12 drop

arg drop

The drop opcode takes one argument and then ignores it. Used to pop an unneeded item off the stack.

### 5.1.13 dup

arg dup arg arg

The dup opcode takes one argument and pushes a second copy to the stack.

## 5.2 Unnamed Opcodes

There is currently only one unnamed opcode.

### 5.2.1 context

#ctx context

The context opcode does not directly appear in the unassembled p-code. It is evaluated from the context reference construct, described below.

## 5.3 Symboled Opcodes

Symboled opcodes are represented in the unassembled p-code as single non-alphanumeric characters, and appear in the assembled p-code as char sized unsigned integers.

### 5.3.1 IF / FI

#boolean {
} #boolean

The { (if) opcode takes one argument from the stack. If the argument evaluates as true, the code following the opcode is executed, otherwise all code until the matching } (fi) opcode is skipped.

The } (fi) opcode terminates an if block, and pushes the logical negation of the originally if'ed value onto the stack.

### 5.3.2 FOR / ROF

[
#boolean ]

The [ (for) opcode marks the beginning of the loop.

The ] (rof) opcode takes one argument from the stack. If the argument evaluates as true, the flow of execution jumps to the matching [ (for) opcode, otherwise the code following the ] (rof) opcode is executed.

### 5.3.3 Binary Logical Opcodes

#arg1 #arg2 oper #result

The binary logical opcodes take two arguments and evaluate them in the form:

result = arg2 oper arg1

These opcodes are as follows:

  less than
  greater than
  = equal
  & and
  | or

### 5.3.4 Binary Arithmetical Opcodes

#arg1 #arg2 oper #result

The binary arithmetical opcodes take two arguments and evaluate them in the form:

result = arg1 oper arg2

These opcodes are as follows:

  + add
  - subtract
  * multiply
  / divide

### 5.3.5 Unary Opcodes

#arg oper #result

The unary opcodes are as follows:

  ! logical negate
  arithmetic negate

## 5.4 Reference Constructs

Reference constructs are higher level functions of the unassembled p-code language that make writing p-code by hand at little bit easier.

There are 4 reference constructs, which are as follows.

### 5.4.1 mark

p-code: @string

compiled: [nothing]

The mark construct marks the start of a p-code segment. It does not have an inline representation in the compiled p-code, instead it is represented in a p-code segment table.

### 5.4.2 reference

p-code: /string

compiled: #index

The reference construct references the start of the named p-code segment. It is represented by a numeric index into the segment table in the compiled p-code.

### 5.4.3 context

p-code: %string

compiled: #index context

The context construct changes the current local nebuloid context. It is represented by a numeric index into the context table followed by the unnamed context opcode in the compiled p-code.

### 5.4.4 module

p-code: &string

compiled: #index

The module construct references a module by name, for use as an argument in the inst opcode.

All module constructs appearing in the p-code are collected into the module table, which is used to determine the index number.

## 5.5 Constant constructs

Strings and numbers.

### 5.5.1 String construct

(Some String)

The string construct is represented by an open bracket (, followed by the string itself, then terminated with a close bracket ). It is copied verbatim into the compiled p-code, brackets included.

### 5.5.2 Integer construct

#12345

The integer construct is represented by a pound (octothorpe) character #, followed by a signed 64-bit value. It is represented in the compiled p-code as an integer opcode followed by 64-bits of signed integer.

Up: HAFTA Overlord P-Code Definition Previous: 4 Header Codes Contents