NewtBC.c

バイトコードの生成 [詳細]

#include <stdio.h>
#include <stdlib.h>
#include "NewtCore.h"
#include "NewtBC.h"
#include "NewtVM.h"
#include "NewtIO.h"
#include "NewtMem.h"

ソースコードを見る。

データ構造

struct  nbc_env_t
 バイトコード環境(構造体定義) [詳細]
struct  freq_func_t
 関数命令テーブル構造体 [詳細]

マクロ定義

#define ENV_BC(env)   ((uint8_t*)env->bytecode.stackp)
 バイトコード
#define ENV_CX(env)   (env->bytecode.sp)
 コードインデックス(プログラムカウンタ)
#define BC   ENV_BC(newt_bc_env)
 作成中のバイトコード
#define CX   ENV_CX(newt_bc_env)
 作成中のコードインデックス(プログラムカウンタ)
#define BREAKSTACK   ((uint32_t*)newt_bc_env->breakstack.stackp)
 ブレークスタック
#define BREAKSP   (newt_bc_env->breakstack.sp)
 ブレークスタックのスタックポインタ
#define ONEXCPSTACK   ((uint32_t*)newt_bc_env->onexcpstack.stackp)
 例外スタック
#define ONEXCPSP   (newt_bc_env->onexcpstack.sp)
 例外スタックのスタックポインタ
#define LITERALS   (newt_bc_env->literals)
 作成中関数オブジェクトのリテラル
#define ARGFRAME   (newt_bc_env->argFrame)
 作成中関数オブジェクトの引数フレーム
#define CONSTANT   (newt_bc_env->constant)
 定数フレーム
#define NBCAddLiteral(r)   NBCAddLiteralEnv(newt_bc_env, r)
 リテラルリストにオブジェクトを追加
#define NBCGenCode(a, b)   NBCGenCodeEnv(newt_bc_env, a, b)
 バイトコードを生成
#define NBCGenCodeL(a, r)   NBCGenCodeEnvL(newt_bc_env, a, r)
 リテラルなオペデータのバイトコードを生成
#define NBCGenPushLiteral(r)   NBCGenPushLiteralEnv(newt_bc_env, r)
 リテラルをプッシュするバイトコードを生成
#define NBCGenBC_op(stree, r)   NBCGenBC_stmt(stree, r, true)
 引数のバイトコードを生成する
#define NBCGenFreq(b)   NBCGenCode(kNBCFreqFunc, b)
 関数命令のバイトコードを生成する

型定義

typedef nbc_env_t nbc_env_t
 バイトコード環境

関数

static int16_t NBCAddLiteralEnv (nbc_env_t *env, newtRefArg r)
static void NBCGenCodeEnv (nbc_env_t *env, uint8_t a, int16_t b)
static void NBCGenCodeEnvL (nbc_env_t *env, uint8_t a, newtRefArg r)
static int16_t NBCGenPushLiteralEnv (nbc_env_t *env, newtRefArg r)
static void NBCGenPUSH (newtRefArg r)
static void NBCGenGetVar (nps_syntax_node_t *stree, newtRefArg r)
static void NBCGenCallFn (newtRefArg fn, int16_t numArgs)
static int16_t NBCMakeFnArgFrame (newtRefArg argFrame, nps_syntax_node_t *stree, nps_node_t r, bool *indefiniteP)
static int16_t NBCMakeFnArgs (newtRefArg fn, nps_syntax_node_t *stree, nps_node_t r)
static nbc_env_tNBCMakeFnEnv (nps_syntax_node_t *stree, nps_node_t args)
static uint32_t NBCGenBranch (uint8_t a)
static void NBCDefLocal (newtRefArg type, newtRefArg r, bool init)
static void NBCBackPatch (uint32_t cx, int16_t b)
static void NBCPushBreakStack (uint32_t cx)
static void NBCBreakBackPatchs (uint32_t loop_head, uint32_t cx)
static void NBCPushOnexcpStack (uint32_t cx)
static void NBCOnexcpBackPatchs (uint32_t try_head, uint32_t cx)
static void NBCGenOnexcpPC (int32_t pc)
static void NBCGenOnexcpBranch (void)
static void NBCOnexcpBackPatchL (uint32_t sp, int32_t pc)
static newtRef NBCMakeFn (nbc_env_t *env)
static void NBCInitFreqFuncTable (void)
static nbc_env_tNBCEnvNew (nbc_env_t *parent)
static void NBCEnvFree (nbc_env_t *env)
static newtRef NBCFnDone (nbc_env_t **envP)
static void NBCInit (void)
static void NBCCleanup (void)
static void NBCGenBC_stmt (nps_syntax_node_t *stree, nps_node_t r, bool ret)
static void NBCGenConstant (nps_syntax_node_t *stree, nps_node_t r)
static void NBCGenGlobalVar (nps_syntax_node_t *stree, nps_node_t r)
static void NBCGenLocalVar (nps_syntax_node_t *stree, nps_node_t type, nps_node_t r)
static bool NBCTypeValid (nps_node_t type)
static int16_t NBCGenTryPre (nps_syntax_node_t *stree, nps_node_t r)
static int16_t NBCGenTryPost (nps_syntax_node_t *stree, nps_node_t r, uint32_t *onexcpspP)
static void NBCGenTry (nps_syntax_node_t *stree, nps_node_t expr, nps_node_t onexception_list)
static void NBCGenIfThenElse (nps_syntax_node_t *stree, nps_node_t cond, nps_node_t thenelse, bool ret)
static void NBCGenAnd (nps_syntax_node_t *stree, nps_node_t op1, nps_node_t op2)
static void NBCGenOr (nps_syntax_node_t *stree, nps_node_t op1, nps_node_t op2)
static void NBCGenLoop (nps_syntax_node_t *stree, nps_node_t expr)
static newtRef NBCMakeTempSymbol (newtRefArg index, newtRefArg val, char *s)
static void NBCGenFor (nps_syntax_node_t *stree, nps_node_t r, nps_node_t expr)
static void NBCGenForeach (nps_syntax_node_t *stree, nps_node_t r, nps_node_t expr)
static void NBCGenWhile (nps_syntax_node_t *stree, nps_node_t cond, nps_node_t expr)
static void NBCGenRepeat (nps_syntax_node_t *stree, nps_node_t expr, nps_node_t cond)
static void NBCGenBreak (nps_syntax_node_t *stree, nps_node_t expr)
static void NBCGenStringer (nps_syntax_node_t *stree, nps_node_t op1, nps_node_t op2, char *dlmt)
static void NBCGenAsign (nps_syntax_node_t *stree, nps_node_t lvalue, nps_node_t expr, bool ret)
static void NBCGenExists (nps_syntax_node_t *stree, nps_node_t r)
static void NBCGenReceiver (nps_syntax_node_t *stree, nps_node_t r)
static void NBCGenMethodExists (nps_syntax_node_t *stree, nps_node_t receiver, nps_node_t name)
static void NBCGenFn (nps_syntax_node_t *stree, nps_node_t args, nps_node_t expr)
static void NBCGenGlobalFn (nps_syntax_node_t *stree, nps_node_t name, nps_node_t fn)
static void NBCGenCall (nps_syntax_node_t *stree, nps_node_t name, nps_node_t args)
static void NBCGenInvoke (nps_syntax_node_t *stree, nps_node_t fn, nps_node_t args)
static void NBCGenFunc2 (nps_syntax_node_t *stree, newtRefArg name, nps_node_t op1, nps_node_t op2)
static void NBCGenSend (nps_syntax_node_t *stree, uint32_t code, nps_node_t receiver, nps_node_t r)
static void NBCGenResend (nps_syntax_node_t *stree, uint32_t code, nps_node_t name, nps_node_t args)
static void NBCGenMakeArray (nps_syntax_node_t *stree, nps_node_t klass, nps_node_t r)
static void NBCGenMakeFrame (nps_syntax_node_t *stree, nps_node_t r)
static void NVCGenNoResult (bool ret)
static void NBCGenSyntaxCode (nps_syntax_node_t *stree, nps_syntax_node_t *node, bool ret)
static int16_t NBCCountNumArgs (nps_syntax_node_t *stree, nps_node_t r)
static newtRef NBCGenMakeFrameSlots_sub (nps_syntax_node_t *stree, nps_node_t r)
static newtRef NBCGenMakeFrameSlots (nps_syntax_node_t *stree, nps_node_t r)
static void NBCGenBC_sub (nps_syntax_node_t *stree, uint32_t n, bool ret)
newtRef NBCGenBC (nps_syntax_node_t *stree, uint32_t size, bool ret)
newtRef NBCCompileFile (char *s, bool ret)
newtRef NBCCompileStr (char *s, bool ret)
void NBError (int32_t err)

変数

static nbc_env_tnewt_bc_env
 バイドコード環境
static freq_func_t freq_func_tb []
 関数命令テーブル


説明

バイトコードの生成

作者:
M.Nukui
日付:
2003-11-07
Copyright (C) 2003-2004 M.Nukui All rights reserved.

NewtBC.c で定義されています。


関数

int16_t NBCAddLiteralEnv ( nbc_env_t env,
newtRefArg  r 
) [static]

リテラルリストにオブジェクトを追加する

引数:
env [in] バイトコード環境
r [in] オブジェクト
戻り値:
追加された位置

NewtBC.c185 行で定義されています。

参照先 nbc_env_t::literalsNcAddArraySlot()NewtArrayLength.

参照元 NBCGenCodeEnvL()NBCGenPushLiteralEnv().

関数の呼び出しグラフ:

void NBCBackPatch ( uint32_t  cx,
int16_t  b 
) [static]

バイトコードをバックパッチする

引数:
cx [in] バックパッチする位置
b [in] バックパッチするオペデータ
戻り値:
なし
 note 分岐命令やループ命令などすぐにオペデータが決定しない場合に使う

NewtBC.c590 行で定義されています。

参照先 BC.

参照元 NBCBreakBackPatchs()NBCGenAnd()NBCGenFor()NBCGenForeach()NBCGenIfThenElse()NBCGenOr()NBCGenTry()NBCGenWhile()NBCOnexcpBackPatchs().

void NBCBreakBackPatchs ( uint32_t  loop_head,
uint32_t  cx 
) [static]

ループ内のブレーク命令をバックパッチする

引数:
loop_head [in] ループの開始位置
cx [in] ループの終わり位置
戻り値:
なし

NewtBC.c629 行で定義されています。

参照先 BREAKSPBREAKSTACKNBCBackPatch().

参照元 NBCGenFor()NBCGenForeach()NBCGenLoop()NBCGenRepeat()NBCGenWhile().

関数の呼び出しグラフ:

void NBCCleanup ( void   )  [static]

バイトコード生成の後始末

戻り値:
なし

NewtBC.c928 行で定義されています。

参照先 NBCEnvFree()newt_bc_envnbc_env_t::parent.

参照元 NBCGenBC().

関数の呼び出しグラフ:

newtRef NBCCompileFile ( char *  s,
bool  ret 
)

ソースファイルをコンパイル

引数:
s [in] ソースファイルのパス
ret [in] 戻り値の有無
戻り値:
関数オブジェクト

NewtBC.c2606 行で定義されています。

参照先 kNewtRefUnbindNBCGenBC()NPSCleanup()NPSParseFile().

参照元 NsCompileFile().

関数の呼び出しグラフ:

newtRef NBCCompileStr ( char *  s,
bool  ret 
)

文字列をコンパイル

引数:
s [in] スクリプト文字列
ret [in] 戻り値の有無
戻り値:
関数オブジェクト

NewtBC.c2634 行で定義されています。

参照先 kNewtRefNILkNewtRefUnbindNBCGenBC()NewtThrow()NPSCleanup()NPSParseStr().

参照元 NsCompile().

関数の呼び出しグラフ:

int16_t NBCCountNumArgs ( nps_syntax_node_t stree,
nps_node_t  r 
) [static]

引数の数をカウントする

引数:
stree [in] 構文木
r [in] 構文木ノード
戻り値:
なし

NewtBC.c2353 行で定義されています。

参照先 nps_syntax_node_t::codekNewtRefUnbindNPSRefIsSyntaxNodeNPSRefToSyntaxNodenps_syntax_node_t::op1nps_syntax_node_t::op2.

参照元 NBCGenCall()NBCGenInvoke()NBCGenMakeArray()NBCGenResend()NBCGenSend().

void NBCDefLocal ( newtRefArg  type,
newtRefArg  r,
bool  init 
) [static]

ローカル変数を定義するバイトコードを生成

引数:
type [in] 変数の型
r [in] 変数名シンボル
init [in] 初期化
戻り値:
なし
覚え書き:
もし init が true ならば初期化するバイトコードを生成する 現在のバージョンでは type は完全に無視される

NewtBC.c559 行で定義されています。

参照先 ARGFRAMEkNewtRefUnbindNBCGenCodeNcSetSlotNewtFindSlotIndex()NewtFprintf().

参照元 NBCGenFor()NBCGenForeach()NBCGenLocalVar().

関数の呼び出しグラフ:

void NBCEnvFree ( nbc_env_t env  )  [static]

バイトコード環境を解放する

引数:
env [in] バイトコード環境
戻り値:
なし

NewtBC.c860 行で定義されています。

参照先 nbc_env_t::breakstacknbc_env_t::bytecodeNewtMemFree()NewtStackFree()nbc_env_t::onexcpstack.

参照元 NBCCleanup()NBCFnDone().

関数の呼び出しグラフ:

nbc_env_t * NBCEnvNew ( nbc_env_t parent  )  [static]

バイトコード環境を作成する

引数:
parent [in] 呼出し元のバイトコード環境
戻り値:
バイトコード環境

NewtBC.c834 行で定義されています。

参照先 nbc_env_t::bytecodeNBCMakeFn()NEWT_NUM_BYTECODENEWT_POOLNewtMemCalloc()NewtStackSetup()nbc_env_t::parent.

参照元 NBCGenBC()NBCMakeFnEnv().

関数の呼び出しグラフ:

newtRef NBCFnDone ( nbc_env_t **  envP  )  [static]

関数オブジェクトの生成を終了する

引数:
envP [in] バイトコード環境へのポインタ
戻り値:
関数オブジェクト

NewtBC.c881 行で定義されています。

参照先 ENV_BCENV_CXnbc_env_t::funckNewtRefNILNBCEnvFree()NBCGenCodeEnv()NcGetSlotNcLength()NcSetSlotNewtMakeBinary()NewtRefIsNotNILNSSYM0nbc_env_t::parent.

参照元 NBCGenBC()NBCGenFn().

関数の呼び出しグラフ:

void NBCGenAnd ( nps_syntax_node_t stree,
nps_node_t  op1,
nps_node_t  op2 
) [static]

論理AND のバイトコードを生成する

引数:
stree [in] 構文木
op1 [in] オペランド1の構文木ノード
op2 [in] オペランド2の構文木ノード
戻り値:
なし

NewtBC.c1313 行で定義されています。

参照先 CXkNewtRefNILNBCBackPatch()NBCGenBC_opNBCGenBranch()NBCGenPUSH().

参照元 NBCGenSyntaxCode().

関数の呼び出しグラフ:

void NBCGenAsign ( nps_syntax_node_t stree,
nps_node_t  lvalue,
nps_node_t  expr,
bool  ret 
) [static]

代入式のバイトコードを生成する

引数:
stree [in] 構文木
lvalue [in] 左辺
expr [in] 式
ret [in] 戻り値の有無
戻り値:
なし

NewtBC.c1793 行で定義されています。

参照先 nps_syntax_node_t::codeCONSTANTkNErrAssignToConstantkNErrSyntaxErrorkNPSArefkNPSGetPathNBCGenBC_opNBCGenCodeNBCGenCodeLNBCGenFreqNBCGenFunc2()NBError()NewtHasSlot()NewtMakeInteger()NewtMPToSymbolNewtRefIsMagicPointerNewtRefIsSymbol()NPSRefIsSyntaxNodeNPSRefToSyntaxNodeNSSYM0NVCGenNoResult()nps_syntax_node_t::op1nps_syntax_node_t::op2.

参照元 NBCGenSyntaxCode().

関数の呼び出しグラフ:

newtRef NBCGenBC ( nps_syntax_node_t stree,
uint32_t  size,
bool  ret 
)

バイトコードの生成

引数:
stree [in] 構文木
size [in] 構文木の長さ
ret [in] 戻り値の有無
戻り値:
関数オブジェクト

NewtBC.c2569 行で定義されています。

参照先 NBCCleanup()NBCEnvNew()NBCFnDone()NBCGenBC_sub()NBCInit()newt_bc_envNEWT_DUMPBCNewtFprintf()NewtPackLiteral()NewtRefIsNotNILNVMDumpFn().

参照元 NBCCompileFile()NBCCompileStr()NVMInterpret2().

関数の呼び出しグラフ:

void NBCGenBC_stmt ( nps_syntax_node_t stree,
nps_node_t  r,
bool  ret 
) [static]

文のバイトコードを生成する

引数:
stree [in] 構文木
r [in] 構文木ノード
ret [in] 戻り値の有無
戻り値:
なし

NewtBC.c952 行で定義されています。

参照先 kNewtRefUnbindNBCGenBC_sub()NBCGenPUSH()NPSRefIsSyntaxNodeNPSRefToSyntaxNode.

参照元 NBCGenFor()NBCGenForeach()NBCGenIfThenElse()NBCGenLoop()NBCGenRepeat()NBCGenSyntaxCode()NBCGenTryPost()NBCGenWhile().

関数の呼び出しグラフ:

void NBCGenBC_sub ( nps_syntax_node_t stree,
uint32_t  n,
bool  ret 
) [static]

バイトコードの生成(再帰呼出し用)

引数:
stree [in] 構文木
n [in] 構文木の長さ
ret [in] 戻り値の有無
戻り値:
なし

NewtBC.c2482 行で定義されています。

参照先 nps_syntax_node_t::codekNPSAddkNPSCallkNPSGetPathkNPSInvokekNPSMakeArraykNPSMakeFramekNPSPopHandlerskNPSResendkNPSResendIfDefinedkNPSSendkNPSSendIfDefinedNBCGenBC_opNBCGenCall()NBCGenCodeNBCGenFreqNBCGenInvoke()NBCGenMakeArray()NBCGenMakeFrame()NBCGenResend()NBCGenSend()NBCGenSyntaxCode()NVCGenNoResult()nps_syntax_node_t::op1nps_syntax_node_t::op2.

参照元 NBCGenBC()NBCGenBC_stmt().

関数の呼び出しグラフ:

uint32_t NBCGenBranch ( uint8_t  a  )  [static]

分岐命令のバイトコードを生成

引数:
a [in] 命令
戻り値:
バイトコードの位置

NewtBC.c534 行で定義されています。

参照先 CXNBCGenCode.

参照元 NBCGenAnd()NBCGenBreak()NBCGenFor()NBCGenForeach()NBCGenIfThenElse()NBCGenOnexcpBranch()NBCGenOr()NBCGenTry()NBCGenWhile().

void NBCGenBreak ( nps_syntax_node_t stree,
nps_node_t  expr 
) [static]

BREAK文のバイトコードを生成する

引数:
stree [in] 構文木
expr [in] 実行文の構文木ノード
戻り値:
なし

NewtBC.c1734 行で定義されています。

参照先 kNewtRefNILkNewtRefUnbindNBCGenBC_opNBCGenBranch()NBCGenPUSH()NBCPushBreakStack().

参照元 NBCGenSyntaxCode().

関数の呼び出しグラフ:

void NBCGenCall ( nps_syntax_node_t stree,
nps_node_t  name,
nps_node_t  args 
) [static]

関数呼出しのバイトコードを生成する

引数:
stree [in] 構文木
name [in] グローバル関数名
args [in] 引数
戻り値:
なし

NewtBC.c2005 行で定義されています。

参照先 NBCCountNumArgs()NBCGenBC_opNBCGenCallFn().

参照元 NBCGenBC_sub().

関数の呼び出しグラフ:

void NBCGenCallFn ( newtRefArg  fn,
int16_t  numArgs 
) [static]

関数呼出しのバイトコードを生成

引数:
fn [in] 関数オブジェクト
numArgs [in] 引数の数
戻り値:
なし

NewtBC.c384 行で定義されています。

参照先 freq_func_tbkNErrWrongNumberOfArgsfreq_func_t::nameNBCGenCodeNBCGenFreqNBCGenPUSH()NBError()NewtRefEqual().

参照元 NBCGenCall()NBCGenExists()NBCGenGlobalVar()NBCGenMethodExists().

関数の呼び出しグラフ:

void NBCGenCodeEnv ( nbc_env_t env,
uint8_t  a,
int16_t  b 
) [static]

バイトコードを生成

引数:
env [in] バイトコード環境
a [in] 命令
b [in] オペデータ
戻り値:
なし

NewtBC.c206 行で定義されています。

参照先 nbc_env_t::bytecodeENV_BCENV_CXkNBCFieldMaskNewtStackExpand().

参照元 NBCFnDone()NBCGenCodeEnvL()NBCGenPushLiteralEnv().

関数の呼び出しグラフ:

void NBCGenCodeEnvL ( nbc_env_t env,
uint8_t  a,
newtRefArg  r 
) [static]

リテラルなオペデータのバイトコードを生成

引数:
env [in] バイトコード環境
a [in] 命令
r [in] オブジェクト
戻り値:
なし

NewtBC.c248 行で定義されています。

参照先 nbc_env_t::literalsNBCAddLiteralEnv()NBCGenCodeEnv()NewtFindArrayIndex()NewtPackLiteral().

関数の呼び出しグラフ:

void NBCGenConstant ( nps_syntax_node_t stree,
nps_node_t  r 
) [static]

定数のバイトコードを生成する

引数:
stree [in] 構文木
r [in] 構文木ノード
戻り値:
なし

NewtBC.c974 行で定義されています。

参照先 nps_syntax_node_t::codeCONSTANTkNErrSyntaxErrorNBError()NcSetSlotNPSRefIsSyntaxNodeNPSRefToSyntaxNodenps_syntax_node_t::op1nps_syntax_node_t::op2.

参照元 NBCGenSyntaxCode().

関数の呼び出しグラフ:

void NBCGenExists ( nps_syntax_node_t stree,
nps_node_t  r 
) [static]

EXISTS式のバイトコードを生成する

引数:
stree [in] 構文木
r [in] 構文木ノード
戻り値:
なし

NewtBC.c1870 行で定義されています。

参照先 nps_syntax_node_t::codekNErrSyntaxErrorkNewtRefTRUEkNPSGetPathLITERALSNBCGenBC_opNBCGenCallFn()NBCGenFreqNBCGenPUSH()NBError()NewtFindArrayIndex()NewtRefIsSymbol()NPSRefIsSyntaxNodeNPSRefToSyntaxNodeNSSYM0nps_syntax_node_t::op1nps_syntax_node_t::op2.

参照元 NBCGenSyntaxCode().

関数の呼び出しグラフ:

void NBCGenFn ( nps_syntax_node_t stree,
nps_node_t  args,
nps_node_t  expr 
) [static]

関数定義のバイトコードを生成する

引数:
stree [in] 構文木
args [in] 引数
expr [in] 式
戻り値:
なし

NewtBC.c1963 行で定義されています。

参照先 NBCFnDone()NBCGenBC_opNBCGenCodeNBCGenPUSH()NBCMakeFnEnv()newt_bc_env.

参照元 NBCGenSyntaxCode().

関数の呼び出しグラフ:

void NBCGenFor ( nps_syntax_node_t stree,
nps_node_t  r,