// compile-h-s.txt
// header file for som-in-som project
//	public release som-v2 30 December 2004
//  som-v2.3  13 June 2006
//  public release som-v24 10 January 2007
//    Celebrating H.M.King 60 years accend to the throne.
//  public release som v3.0 5 March 2007 (Maka-bucha day)
//  public release som v3.1 19 Aug 2007  (Draft vote day)

// sequence of loading files
//
// lib.som
// string-s.txt
// compile-h-s.txt
// list-s.txt
// symtab-s.txt
// token-s.txt
// parse-h-s.txt
// stmt-s.txt
// parse.som
// icode-s.txt
// callchain-s.txt
// gencode-s.txt
// macro-s.txt
// main-s.txt

enum	// marker
    0 NIL MARK BMARK
enum	// atom
    0 SP OPER NUM GNAME LNAME CNAME STRING ADS
enum	// sx-code
    1 icAdd icSub icMul icDiv icBand icBor icBxor icMod
    icEq icNe icLt icLe icGe icGt icShl icShr
    // addv... 21..40
    // addi... 41..60
enum
    61 icGet icPut icLd icSt icLdx icStx icLdy icSty
    icJmp icJt icJf icCall icRet icR1 icEfor
    icInc icDec icLit icAds icSys icFun icCalli
    // zero arg
    icNot icCase icEnd
    // special
    icGet0 icPut0 icLd0 icLit0 icAds0 icCall0 icSys0
enum
    86 EOC

enum	// offset to v-group
    20 OPV
enum	// offset to im-group
    40 OPM
enum	// token type
    14 tkIDEN tkNUMBER tkSTRING tkEOF tkERROR
enum	// token
    50 tkSTAR tkSLASH tkMINUS tkPLUS tkEQ tkEQEQ
    tkAND tkBAR tkCARET tkMOD tkNOT tkNE tkLT tkLE
    tkLTLT tkGT tkGE tkGTGT tkCOLON tkLPAREN tkRPAREN
    tkLBRACKET tkRBRACKET tkBB tkBE tkTO tkIF tkELSE
    tkWHILE tkFOR tkBREAK tkARRAY tkCASE tkENUM tkSYSCALL
enum	// symbol type
    6 tyFUNC tyLOCAL tyGVAR tyENUM tyKEY tyNEW tyMAC tyFULL
enum	// denote gvar arg for DS relocation
    0 VSCALAR VARRAY VSTRING
enum
    5678931 OBJ_SOM31
enum	// op type for gencode
    50 xLV xGV xLIT xEX xCM xLG xJP

//  memory model
//  M [ MAXMEM  ]		 declared in C
//  M[1..MAXSYS]  		 system area (in eval-s)
//  XOP[MAXCS]    		 code segment
//	XARG[MAXCS]
//       ^
//       CP
//  ystack[MAXYSTK]		 parser stack
//           ^
//          ysp
//  cell[MAXCELL]		 store list (parse-tree)
//  data segment is absolute M[]

enum
    10000 MAXCS		// size of code segment
enum
    1000 MACSIZE	// size of macro
enum
    2000 MAXYSTK	// size of parser stack
enum
    200  MAXCALL	// size of call forward list

//  system area  1 .. MAXSYS-1
//   101     mode ads
enum
    101 mode_ads

to initSom | a =
    M = 0				// base of M, alias with C M[]
    CP = 3  			// code pointer
    XOP = array MAXCS	// new code segment
    XARG = array MAXCS
    ystack = array MAXYSTK  // parser stack
    ysp = 0
    currentf = 0		// current fn idx
    nenum = 0			// enum number
    verbose = 1			// control compiler message
    cmode = 0			// compile mode, 0 def, 1 im, 2 rep
    callis = array MAXCALL	// list of call forward
    reflis = array MAXCALL
    ufunlis = array 200	// list of unconverted fun
    CPX = MAXCS-MACSIZE	// immediate & macro code area
    exsym = array 500 	// export symbol
    nxsym = 0
    line = 0
    FI = 0				// input file
    FO = 1				// output file
    lv2 = array 100		// for rename
    vrec = array 100
    jplis = array 1000	// ads of icJmp

: fspace = fprintc FO 32
: fnl = fprintc FO 10

// when no line number is available (gencode, eval, etc.)
to error x =
    prints "error: " prints x nl
    exit

// error during parsing
to seterror s =
    prints "line " print line space
    prints s nl
    exit

to warning nm mess =
    if verbose
        prints "Warning: "
        if nm != 0
            prints nm space
        prints mess nl

: isV op = op_type[op] == xLV
: isJmp op = op_type[op] == xJP
: isCommute op = op_type[op] == xCM
: isLogic op = op_type[op] == xLG
: isGlobal op = op_type[op] == xGV

// End
