Some notes on structure mapping between OPL and C/C++
-----------------------------------------------------

Alignment

This is straightforward - both OPL and Topspeed C byte-align
variables. In the case of Topspeed C, this applies to variables
declared both within and outwith structures. In the case of OPL,
byte-alignment applies to both global and local declarations.


Byte ordering

Both OPL and Topspeed C store multi-byte integral types with the
Least Significant Byte first, and the Most Significant Byte last.
No surprise here given that they are both running on Intel chips!


Data representation

short == intvar% and both are two's complement 16-bit
quantities.

double == floatvar and both are 64-bit IEEE floating point
representations.

long == longvar& and both are two's complement 32-bit
quantities.

'int' is synonymous with 'short' in the Topspeed C
implementation. All OPL integral types are signed.

Any (non-function) pointer type in C is best mapped to an OPL
intvar% (both are two bytes). Then use OPL's peek, poke etc. for
the indirection. Use uadd() and usub() (if available) when doing
pointer arithmetic in OPL.

There is nothing in OPL that corresponds to the C 'char' type. So
it's not possible to model a C struct like:

  struct Sample
  {
    short a;
    char  b;
    short c;
  }

in OPL. Fortunately, this is never necessary AFAIK.


Strings. The OPL:

  global name$(n)

is equivalent to:

  struct String
  {
    unsigned char max_length = n;
    unsigned char current_length = 0;
    char character[n];
  }

In both cases (n+2) contiguous bytes of storage are reserved. It
follows from this that OPL strings cannot be longer than 255
characters.

The OPL addr() function returns the address of the
'current_length' field - the 'max_length' field is hidden.

For example:

  proc strinfo:(a%)
  print "The string is '";peek$(a%);"'"
  print "Its current length is", peekb(a%)
  print "Its maximum length is", peekb(a%-1)
  endp

  local name$(12)
  name$="David"
  strinfo:(addr(name$))

Note that one cannot in general depend on the 'maximum_length'
byte preceding the addr() of the string. In particular it will
not be there for string array elements other than the first (see
below).


Arrays. The OPL:

  global intvar%(n)

maps to:

  struct Array
  {
    unsigned short max_index = n;
    short element[n];
  }

where the OPL index is in range [1,n], and the C index in range
[0,n-1] as usual. For arrays of other numeric types the same
layout is followed, only the size of the base element differs.

Given an OPL array 'global n%(4)' both addr(n%()) and addr(n%(1))
return the address of the first element (@element[0] in C).


Arrays of strings. The OPL:

  global text$(n, m)

which declares an array of 'n' strings each of maximum length 'm'
maps to:

  struct StringArray
  {
    unsigned short max_index = n;
    unsigned char max_length = m;
    struct StringArrayElement element[n];
  }

where:

  struct StringArrayElement
  {
    unsigned char current_length;
    char character[m];
  }

so a total of 3+n*(m+1) bytes are allocated. For example:

  local name$(2,4)

is laid out in memory as follows:

Byte offset    Contents         Size (bytes)
0              max_index=2      2
2              max_length=4     1

3              current_length   1 } First
4              characters       4 } string

8              current_length   1 } Second
9              characters       4 } string

All the 'current_length' bytes will contain zero initially.


OPL structures

OPL has no structures (or any aggregate data type other than the
array, for that matter). It is necessary to fake them by
declaring variables consecutively and referring to the result by
the address of the first variable.

For example:

  proc memfree&:
  REM Return free space on internal drive (in bytes)
  REM Structure for FilStatusDevice
  local version%, type%, remove%, size&, free&, name$(63)

  name$="loc::m:"+chr$(0)
  call($0A87,UADD(addr(name$),1),addr(version%),0,0,0)

  return free&
  endp

Here the nameless structure is passed in the 'call' statement by
the address of its first member. Note also that most EPOC system
services expects C-style zero-terminated strings, which is why:

  a) A zero-value byte is appended to the name$ string;

  b) The address passed is one greater than the address of the
  string as returned by OPL. This skips the 'current_length'
  byte, which means nothing to non-OPL routines.

The OPL pseudo-structure above corresponds to the C:

  struct FilStatusDevice
  {
    short version;
    short type;
    short remove;
    long size;
    long free;
    /* And now the 'name' string */
    unsigned char max_length;
    unsigned char current_length;
    char character[63];
  }


----------------
David Palmer
Edinburgh
October 29, 1994

