Som v1.5 macro expansion

The macro expansion in som v1.5 has two types:  a simple macro and a full macro.  A simple macro has no assignment in the macro body hence it can be expanded without creating any new local variable.  For example,

: print x = syscall {1 x}

with the call

to main...
  ...
  print 32
  ...

the macro is expanded into

to main ...
  ...
  syscall {1 32}
  ..

For a full macro, the local variables in the body of the macro must be appended to the list of local variables of the caller,
that is, extending the environment of the caller.  The actual arguments to the macro must be evaluated and instatiated into the local variables.

The expansion is:

check type simple/full
if simple then
substitute macro body with arg list
else it is full do
1 generate arg list
2 add macro arg to no.of.arg of the current function
3 generate param passing (icPut #v)
4 generate macro body with rename the arg of macro body (for get put inc dec)

To decide whether a macro is simple or full
it is simple when there is no assignment (checking by detecting no icPut)
otherwise it is full

Example of simple macros (in lib.som)

: print x = syscall {1 x}
: printc x = syscall {2 x}
: space = syscall {2 32}
: nl = syscall {2 10}
: or a b = if a 1 else b
: and a b = if a b else 0

Example of full macros

// sum 1..n
: sum n | i s =
  i = 1
  while i <= n
    s = s + i
    i = i + 1
  s

END

last update  26 June 2005