/*乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//
    bolcom.h
//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗*/

#define  WINAPI  __stdcall
#define  WINAPIV __cdecl

#define  LOCALF  static __stdcall
#define  LOCALFV static __cdecl

#ifdef __cplusplus
extern "C" {
#endif

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

#ifdef OWN_USE
enum {
  E_STK = 20,
         E_SEG, E_OPC, E_DIV, E_NUL, E_ORD, E_EXE, E_BCD, E_VER, E_PRO, // 20- 29
  E_NAC, E_TKN, E_END, E_DLM, E_UNX, E_IDN, E_UNK, E_DUP, E_VOI, E_SCA, // 30- 39
  E_CST, E_OPR, E_ASN, E_OBJ, E_IMM, E_PTR, E_STU, E_NME, E_MUL, E_TYP, // 40- 49
  E_ARY, E_FNC, E_JMP, E_INT, E_SWI, E_ARI, E_INI, E_AUT, E_LIT, E_LEN, // 50- 59
  E_ASZ, E_CPL, E_ENT,                                                  // 60- 62
};
#endif

typedef void (CALLBACK *BFNC)(pBIN sp);

void WINAPI fi_0(pBIN sp);

typedef struct {
  BFNC  *fn;
  I4B   ni;
} BFNS;
typedef BFNS *pBFNS;
typedef const BFNS *rBFNS;

BOOL WINAPI RegisterAPIs(rBFNS bf);

#ifdef OWN_USE

typedef struct {
  I4B   ofs;
  U2B   asz;
  U2B   bsz;
} CFNI;
typedef CFNI *pCFNI;
typedef const CFNI *rCFNI;

typedef struct {
  pCFNI fn;
  I4B   ni;
} CFNS;
typedef CFNS *pCFNS;
typedef const CFNS *rCFNS;

typedef struct {
  pBIN  sp, bp;
  pBIN  ss, se;
  pBIN  cs, ce;
  pBIN  ds, de;
  pBIN  es, ee;
  rBFNS bf;
  rCFNS cf;
} HEXE_;
typedef HEXE_ EXEE, *HEXE;

HEXE WINAPI CreateExecEnv(I4B ssz, I4B dsz, I4B esz, rBFNS bf, rCFNS cf);
void WINAPI DeleteExecEnv(HEXE xe);

#define  NSX_BFN  0x20000000
#define  NSX_CFN  0x30000000

#define  TPX_DAT  0x02000000
#define  TPX_BSS  0x03000000

enum {
  end_v,   rsv01,   rsv02,   ent_w,   drp_i,   drp_d,   swp_i,   swp_d,   // 00-07
  swp_d_i, swp_i_d, swp_b,   swp_w,   dup_i,   dup_d,   psh_i_0, psh_d_0, // 08-0F
  psh_c,   psh_b,   psh_s,   psh_w,   psh_i,   psh_l,   psh_f,   psh_d,   // 10-17
  bfn_b,   bfn_w,   psh_bs,  psh_bi,  psh_dw,  psh_du,  psh_ew,  psh_eu,  // 18-1F
  fch_c,   fch_b,   fch_s,   fch_w,   fch_i,   fch_l,   fch_f,   fch_d,   // 20-27
  cfn_b,   cfn_w,   fch_bs,  fch_bi,  fch_dw,  fch_du,  fch_ew,  fch_eu,  // 28-2F
  sto_c,   sto_s,   sto_i,   sto_l,   sto_f,   sto_d,   rsv36,   rsv37,   // 30-37
  sto_c_v, sto_s_v, sto_i_v, sto_l_v, sto_f_v, sto_d_v, rsv3E,   rsv3F,   // 38-3F
  add_i,   add_i_b, add_i_w, add_i_i, add_l,   add_d,   add_p_i, add_i_p, // 40-47
  mul_i,   mul_i_b, mul_i_w, mul_i_i, mul_l,   mul_d,   jeq_i_0, jne_i_0, // 48-4F
  sub_i,   sub_i_b, sub_i_w, sub_i_i, sub_l,   sub_d,   sub_p_i, sub_p,   // 50-57
  div_i,   div_i_b, div_i_w, div_i_i, div_l,   div_d,   jan_i,   jor_i,   // 58-5F
  shr_i,   shr_i_b, shr_u,   shr_u_b, shr_l,   shr_q,   not_i,   not_l,   // 60-67
  shl_i,   shl_i_b, shl_l,   mod_i,   mod_i_b, mod_l,   nop_v,   jmp_v,   // 68-6F
  and_i,   and_i_b, and_i_w, and_i_i, and_l,   neg_i,   neg_l,   neg_d,   // 70-77
  or_i,    or_i_b,  or_i_w,  or_i_i,  or_l,    xor_i,   xor_i_i, xor_l,   // 78-7F
  cnv_i_c, cnv_i_b, cnv_i_s, cnv_i_w, cnv_i_l, cnv_i_d, cnv_u_l, cnv_u_d, // 80-87
  cnv_l_c, cnv_l_b, cnv_l_s, cnv_l_w, cnv_l_i, cnv_l_u, cnv_l_d, cnv_q_d, // 88-8F
  cnv_d_c, cnv_d_b, cnv_d_s, cnv_d_w, cnv_d_i, cnv_d_u, cnv_d_l, cnv_d_q, // 90-97
  hib_i,   hiw_i,   mkw_i,   mku_i,   chk_b,   chk_w,   chk_c,   chk_s,   // 98-9F
  lt_i,    lt_u,    lt_l,    lt_q,    lt_d,    lt_i_0,  lt_l_0,  lt_d_0,  // A0-A7
  le_i,    le_u,    le_l,    le_q,    le_d,    le_i_0,  le_l_0,  le_d_0,  // A8-AF
  gt_i,    gt_u,    gt_l,    gt_q,    gt_d,    gt_i_0,  gt_l_0,  gt_d_0,  // B0-B7
  ge_i,    ge_u,    ge_l,    ge_q,    ge_d,    ge_i_0,  ge_l_0,  ge_d_0,  // B8-BF
  eq_i,    eq_l,    eq_d,    eq_i_0,  eq_d_0,  fnc_v,   fnc_i,   fnc_d,   // C0-C7
  ne_i,    ne_l,    ne_d,    ne_i_0,  ne_d_0,  ret_v,   ret_i,   ret_d,   // C8-CF
  inc_c,   inc_s,   inc_i,   inc_l,   inc_f,   inc_d,   inc_p,   inc_i_i, // D0-D7
  inc_c_v, inc_s_v, inc_i_v, inc_l_v, inc_f_v, inc_d_v, inc_p_v, inc_p_p, // D8-DF
  dec_c,   dec_s,   dec_i,   dec_l,   dec_f,   dec_d,   dec_p,   dec_i_i, // E0-E7
  dec_c_v, dec_s_v, dec_i_v, dec_l_v, dec_f_v, dec_d_v, dec_p_v, dec_p_p, // E8-EF
};

I4B  WINAPI cmdsiz(I4B oc);

typedef struct {
  pBIN  pb;
  I4B   sz;
} SEGS;
typedef SEGS *pSEGS;
typedef const SEGS *rSEGS;

void WINAPI SetExecCS(HEXE xe, rSEGS pc);
void WINAPI SetExecDS(HEXE xe, rSEGS pd, rIDXS pr);

void WINAPI CallExecX(pANY pv, HEXE xe, I4B ofs, rANY arg, I4B asz, I4B bsz);
void WINAPI CallExecV(pANY pv, HEXE xe, I4B ent, rANY arg, I4B asz);
void WINAPI CallExec(pANY pv, HEXE xe, I4B ent, rANY arg);

typedef struct {
  I4B   app;
  I4B   pro;
  I4B   ent;
  I4B   ssz;
  HEXE  xe;
  SEGS  cs;
  SEGS  ds;
  I4B   esz;
  IDXS  rs;
  CFNS  fs;
} HBCD_;
typedef HBCD_ BCDE, *HBCD;

#define  DEFSSZ  131072

HBCD WINAPI CreateBCD(I4B app, I4B pro, I4B ent, I4B ssz, rSEGS pc, rSEGS pd, I4B esz, rIDXS pr);

#else

typedef struct { I4B unused;} HBCD_;
typedef HBCD_ *HBCD;

#endif

void WINAPI DeleteBCD(HBCD hb);

#ifdef OWN_USE
BOOL WINAPI AttachBCD(HBCD hb);
#endif

typedef struct {
  I4B   app;
  I4B   pro;
  I4B   ent;
  U4B   rsv;
  I1B   tit[32];
  I1B   dsc[64];
} BCDH;
typedef BCDH *pBCDH;

BOOL WINAPI ReadBCDHeader(pBCDH ph, rSTR nam);

HBCD WINAPI LoadBCD(rSTR nam, I4B app, I4B pro, I4B ssz);
#define  UnloadBCD(hb)  DeleteBCD(hb)

#ifdef OWN_USE
BOOL WINAPI SaveBCD(rSTR nam, HBCD hb, rSTR tit, rSTR dsc);
#endif

void WINAPI CallBCD(pANY pv, HBCD hb, I4B ent, rANY arg);
void WINAPI ExecBCD(pANY pv, HBCD hb, rANY arg);

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

#ifdef OWN_USE

#define  DISABLE_OPT_1  0x01
#define  DISABLE_OPT_2  0x02
#define  DISABLE_OPT_3  0x04
#define  DISABLE_OPT_4  0x08
#define  DISABLE_OPT_5  0x10
#define  DISABLE_OPT_6  0x20
#define  DISABLE_OPT_7  0x40

void WINAPI OptimizeBCD(pSEGS pc, I4B opt);

#endif

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

#ifdef OWN_USE

#define  CreateCodBlk(cap)  CreateBinBlk(cap)
#define  DeleteCodBlk(bb)  DeleteBinBlk(bb)

HBLK WINAPI SelectCodBlk(HBLK bb);

void WINAPIV outcod(I4B oc, ...);

I4B  WINAPI codlen(void);
void WINAPI setcodlen(I4B ln);
void WINAPI fixcod1(I4B ln, I4B va);
void WINAPI fixcod2(I4B ln, I4B va);
void WINAPI fixcod4(I4B ln, I4B va);
void WINAPI xfercodblk(HBLK bb, I4B ln);
void WINAPI outcodblk(HBLK bb);

#define  SW_CNS  0
#define  SW_DAT  2
#define  SW_BSS  3

BOOL WINAPI iscnsexpr(HBLK bb, I4B sw);
void WINAPI evalcnsexpr(pANY pv, HBLK bb, I4B sw);

#endif

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

#define  LIDN  32
#define  LLIT  4096

pSTR WINAPI unescb(pSTR bf, rSTR st);

rSTR WINAPI unesc(rSTR st);

#ifdef OWN_USE
rCODS WINAPI test_keywords(void);
rENMS WINAPI test_reserved(void);
#endif

pSTR WINAPI strcpyi(pSTR sd, rSTR ss);
pSTR WINAPI strcpyl(pSTR sd, rSTR ss);

#ifdef OWN_USE
BOOL WINAPI strlitconc(pBIN bf, I4B sz);
#endif

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

#ifdef OWN_USE

typedef struct {
  HBLK  sym;
  HBLK  sbk;
  HBLK  typ;
  HBLK  idn;
  HBLK  cod;
  HBLK  cbk;
  HBLK  def;
  HBLK  dat;
  HBLK  rel;
  I4B   sof;
  I4B   aof;
  I4B   nst;
  I4B   nbf;
  I4B   ncf;
  I4B   cfn;
  I4B   flg;
  I4B   ent;
  U4B   rsv;
} HCPL_;
typedef HCPL_ CPLE, *HCPL;

HCPL WINAPI CreateCmplEnv(I4B csz, I4B dsz);
void WINAPI DeleteCmplEnv(HCPL ce);
HCPL WINAPI SelectCmplEnv(HCPL ce);

I4B  WINAPI outdat(rSEGS ds, rIDXS rl);
I4B  WINAPI outstr(rSTR st);

#define  DEFCSZ  131072
#define  DEFDSZ  131072

#endif

typedef struct {
  rSTR  hfn;
  rSTR  sfn;
  rSTR  ofn;
  rSTR  mai;
  I4B   app;
  I4B   pro;
  rSTR  tit;
  rSTR  dsc;
  HBLK  hxb;
  HBLK  sxb;
  HBCD  *ph;
  I4B   opt;
} CPLI;
typedef CPLI *pCPLI, *PCPLI;
typedef const CPLI *rCPLI, *PCCPLI;

BOOL WINAPI CompileBCD(rCPLI cp);

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//

U4B  WINAPI BOLCOMVer(void);

BOOL WINAPI InitBOLCOM(uANY pa);
void WINAPI TermBOLCOM(void);

#ifdef __cplusplus
}
#endif

//乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗乗//
