changed awk to represent an integer of a certain range in a pointer directly
This commit is contained in:
parent
fe1ff5cdba
commit
aed205c298
@ -2553,10 +2553,6 @@ QSE_EXPORT void qse_awk_rtx_refdownval_nofree (
|
||||
qse_awk_val_t* val /**< value pointer */
|
||||
);
|
||||
|
||||
|
||||
#define qse_awk_rtx_getvaltype(rtx,v) ((v)->v_type)
|
||||
#define qse_awk_rtx_getintfromval(rtx,v) (((qse_awk_val_int_t*)(v))->i_val)
|
||||
|
||||
/**
|
||||
* The qse_awk_rtx_valtobool() function converts a value \a val to a boolean
|
||||
* value.
|
||||
|
@ -360,7 +360,6 @@ struct qse_awk_mod_data_t
|
||||
qse_awk_mod_t mod;
|
||||
};
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
@ -874,17 +874,17 @@ int qse_awk_fnc_toupper (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
static int __substitute (qse_awk_rtx_t* rtx, qse_awk_int_t max_count)
|
||||
{
|
||||
qse_size_t nargs;
|
||||
qse_awk_val_t* a0, * a1, * a2, * v;
|
||||
qse_awk_val_type_t a0_vtype, a1_vtype;
|
||||
qse_awk_val_type_t a0_vtype;
|
||||
|
||||
qse_cstr_t s0, s1, s2;
|
||||
qse_cstr_t s0, s2;
|
||||
qse_cstr_t s1 = { QSE_NULL, 0 };
|
||||
const qse_char_t* s2_end;
|
||||
|
||||
qse_char_t* s0_free = QSE_NULL;
|
||||
qse_char_t* s1_free = QSE_NULL;
|
||||
qse_char_t* s2_free = QSE_NULL;
|
||||
|
||||
void* rex = QSE_NULL;
|
||||
@ -897,20 +897,19 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
|
||||
qse_awk_int_t sub_count;
|
||||
|
||||
nargs = qse_awk_rtx_getnargs (run);
|
||||
nargs = qse_awk_rtx_getnargs (rtx);
|
||||
QSE_ASSERT (nargs >= 2 && nargs <= 3);
|
||||
|
||||
a0 = qse_awk_rtx_getarg (run, 0);
|
||||
a1 = qse_awk_rtx_getarg (run, 1);
|
||||
a2 = (nargs >= 3)? qse_awk_rtx_getarg (run, 2): QSE_NULL;
|
||||
a0 = qse_awk_rtx_getarg (rtx, 0);
|
||||
a1 = qse_awk_rtx_getarg (rtx, 1);
|
||||
a2 = (nargs >= 3)? qse_awk_rtx_getarg (rtx, 2): QSE_NULL;
|
||||
|
||||
a0_vtype = qse_awk_rtx_getvaltype (rtx, a0);
|
||||
a1_vtype = qse_awk_rtx_getvaltype (rtx, a1);
|
||||
QSE_ASSERT (a2 == QSE_NULL || qse_awk_rtx_getvaltype(rtx, a2) == QSE_AWK_VAL_REF);
|
||||
|
||||
if (a0_vtype == QSE_AWK_VAL_REX)
|
||||
{
|
||||
rex = ((qse_awk_val_rex_t*)a0)->code[run->gbl.ignorecase];
|
||||
rex = ((qse_awk_val_rex_t*)a0)->code[rtx->gbl.ignorecase];
|
||||
}
|
||||
else if (a0_vtype == QSE_AWK_VAL_STR)
|
||||
{
|
||||
@ -919,39 +918,30 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
}
|
||||
else
|
||||
{
|
||||
s0.ptr = qse_awk_rtx_valtostrdup (run, a0, &s0.len);
|
||||
s0.ptr = qse_awk_rtx_valtostrdup (rtx, a0, &s0.len);
|
||||
if (s0.ptr == QSE_NULL) goto oops;
|
||||
s0_free = (qse_char_t*)s0.ptr;
|
||||
}
|
||||
|
||||
if (a1_vtype == QSE_AWK_VAL_STR)
|
||||
{
|
||||
s1.ptr = ((qse_awk_val_str_t*)a1)->val.ptr;
|
||||
s1.len = ((qse_awk_val_str_t*)a1)->val.len;
|
||||
}
|
||||
else
|
||||
{
|
||||
s1.ptr = qse_awk_rtx_valtostrdup (run, a1, &s1.len);
|
||||
if (s1.ptr == QSE_NULL) goto oops;
|
||||
s1_free = (qse_char_t*)s1.ptr;
|
||||
}
|
||||
s1.ptr = qse_awk_rtx_getvalstr (rtx, a1, &s1.len);
|
||||
if (s1.ptr == QSE_NULL) goto oops;
|
||||
|
||||
if (a2 == QSE_NULL)
|
||||
{
|
||||
/* is this correct? any needs to use inrec.d0? */
|
||||
s2.ptr = QSE_STR_PTR(&run->inrec.line);
|
||||
s2.len = QSE_STR_LEN(&run->inrec.line);
|
||||
s2.ptr = QSE_STR_PTR(&rtx->inrec.line);
|
||||
s2.len = QSE_STR_LEN(&rtx->inrec.line);
|
||||
}
|
||||
else
|
||||
{
|
||||
s2.ptr = qse_awk_rtx_valtostrdup (run, a2, &s2.len);
|
||||
s2.ptr = qse_awk_rtx_valtostrdup (rtx, a2, &s2.len);
|
||||
if (s2.ptr == QSE_NULL) goto oops;
|
||||
s2_free = (qse_char_t*)s2.ptr;
|
||||
}
|
||||
|
||||
if (qse_str_init (&new, run->awk->mmgr, s2.len) <= -1)
|
||||
if (qse_str_init (&new, rtx->awk->mmgr, s2.len) <= -1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
new_inited = 1;
|
||||
@ -961,14 +951,14 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
qse_awk_errnum_t errnum;
|
||||
int x;
|
||||
|
||||
if (run->gbl.ignorecase)
|
||||
x = qse_awk_buildrex (run->awk, s0.ptr, s0.len, &errnum, QSE_NULL, &rex);
|
||||
if (rtx->gbl.ignorecase)
|
||||
x = qse_awk_buildrex (rtx->awk, s0.ptr, s0.len, &errnum, QSE_NULL, &rex);
|
||||
else
|
||||
x = qse_awk_buildrex (run->awk, s0.ptr, s0.len, &errnum, &rex, QSE_NULL);
|
||||
x = qse_awk_buildrex (rtx->awk, s0.ptr, s0.len, &errnum, &rex, QSE_NULL);
|
||||
|
||||
if (x <= -1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, errnum, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
|
||||
@ -994,7 +984,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
if (max_count == 0 || sub_count < max_count)
|
||||
{
|
||||
n = qse_awk_matchrex (
|
||||
run->awk, rex, run->gbl.ignorecase,
|
||||
rtx->awk, rex, rtx->gbl.ignorecase,
|
||||
&s2, &cur, &mat, &errnum
|
||||
);
|
||||
}
|
||||
@ -1002,7 +992,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
|
||||
if (n <= -1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, errnum, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, errnum, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
|
||||
@ -1012,7 +1002,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
if (qse_str_ncat (
|
||||
&new, cur.ptr, cur.len) == (qse_size_t)-1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
break;
|
||||
@ -1030,7 +1020,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
if (qse_str_ncat (
|
||||
&new, cur.ptr, mat.ptr - cur.ptr) == (qse_size_t)-1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
|
||||
@ -1054,7 +1044,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
|
||||
if (m == (qse_size_t)-1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
}
|
||||
@ -1073,7 +1063,7 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
m = qse_str_ncat (&new, cur.ptr, 1);
|
||||
if (m == (qse_size_t)-1)
|
||||
{
|
||||
qse_awk_rtx_seterrnum (run, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
qse_awk_rtx_seterrnum (rtx, QSE_AWK_ENOMEM, QSE_NULL);
|
||||
goto oops;
|
||||
}
|
||||
|
||||
@ -1083,10 +1073,10 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
|
||||
if (rex_free)
|
||||
{
|
||||
if (run->gbl.ignorecase)
|
||||
qse_awk_freerex (run->awk, QSE_NULL, rex_free);
|
||||
if (rtx->gbl.ignorecase)
|
||||
qse_awk_freerex (rtx->awk, QSE_NULL, rex_free);
|
||||
else
|
||||
qse_awk_freerex (run->awk, rex_free, QSE_NULL);
|
||||
qse_awk_freerex (rtx->awk, rex_free, QSE_NULL);
|
||||
rex_free = QSE_NULL;
|
||||
}
|
||||
|
||||
@ -1095,57 +1085,59 @@ static int __substitute (qse_awk_rtx_t* run, qse_awk_int_t max_count)
|
||||
if (a2 == QSE_NULL)
|
||||
{
|
||||
int n;
|
||||
n = qse_awk_rtx_setrec (run, 0, QSE_STR_XSTR(&new));
|
||||
n = qse_awk_rtx_setrec (rtx, 0, QSE_STR_XSTR(&new));
|
||||
if (n <= -1) goto oops;
|
||||
}
|
||||
else
|
||||
{
|
||||
int n;
|
||||
|
||||
v = qse_awk_rtx_makestrvalwithxstr (run, QSE_STR_XSTR(&new));
|
||||
v = qse_awk_rtx_makestrvalwithxstr (rtx, QSE_STR_XSTR(&new));
|
||||
if (v == QSE_NULL) goto oops;
|
||||
qse_awk_rtx_refupval (run, v);
|
||||
n = qse_awk_rtx_setrefval (run, (qse_awk_val_ref_t*)a2, v);
|
||||
qse_awk_rtx_refdownval (run, v);
|
||||
qse_awk_rtx_refupval (rtx, v);
|
||||
n = qse_awk_rtx_setrefval (rtx, (qse_awk_val_ref_t*)a2, v);
|
||||
qse_awk_rtx_refdownval (rtx, v);
|
||||
if (n <= -1) goto oops;
|
||||
}
|
||||
}
|
||||
|
||||
qse_str_fini (&new);
|
||||
|
||||
if (s2_free) QSE_AWK_FREE (run->awk, s2_free);
|
||||
if (s1_free) QSE_AWK_FREE (run->awk, s1_free);
|
||||
if (s0_free) QSE_AWK_FREE (run->awk, s0_free);
|
||||
if (s2_free) QSE_AWK_FREE (rtx->awk, s2_free);
|
||||
|
||||
v = qse_awk_rtx_makeintval (run, sub_count);
|
||||
qse_awk_rtx_freevalstr (rtx, a1, s1.ptr);
|
||||
|
||||
if (s0_free) QSE_AWK_FREE (rtx->awk, s0_free);
|
||||
|
||||
v = qse_awk_rtx_makeintval (rtx, sub_count);
|
||||
if (v == QSE_NULL) return -1;
|
||||
|
||||
qse_awk_rtx_setretval (run, v);
|
||||
qse_awk_rtx_setretval (rtx, v);
|
||||
return 0;
|
||||
|
||||
oops:
|
||||
if (rex_free)
|
||||
{
|
||||
if (run->gbl.ignorecase)
|
||||
qse_awk_freerex (run->awk, QSE_NULL, rex_free);
|
||||
if (rtx->gbl.ignorecase)
|
||||
qse_awk_freerex (rtx->awk, QSE_NULL, rex_free);
|
||||
else
|
||||
qse_awk_freerex (run->awk, rex_free, QSE_NULL);
|
||||
qse_awk_freerex (rtx->awk, rex_free, QSE_NULL);
|
||||
}
|
||||
if (new_inited) qse_str_fini (&new);
|
||||
if (s2_free) QSE_AWK_FREE (run->awk, s2_free);
|
||||
if (s1_free) QSE_AWK_FREE (run->awk, s1_free);
|
||||
if (s0_free) QSE_AWK_FREE (run->awk, s0_free);
|
||||
if (s2_free) QSE_AWK_FREE (rtx->awk, s2_free);
|
||||
if (s1.ptr) qse_awk_rtx_freevalstr (rtx, a1, s1.ptr);
|
||||
if (s0_free) QSE_AWK_FREE (rtx->awk, s0_free);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int qse_awk_fnc_gsub (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi)
|
||||
int qse_awk_fnc_gsub (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
{
|
||||
return __substitute (run, 0);
|
||||
return __substitute (rtx, 0);
|
||||
}
|
||||
|
||||
int qse_awk_fnc_sub (qse_awk_rtx_t* run, const qse_awk_fnc_info_t* fi)
|
||||
int qse_awk_fnc_sub (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
{
|
||||
return __substitute (run, 1);
|
||||
return __substitute (rtx, 1);
|
||||
}
|
||||
|
||||
int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
@ -1169,24 +1161,10 @@ int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
qse_awk_val_t* a2;
|
||||
|
||||
a2 = qse_awk_rtx_getarg (rtx, 2);
|
||||
#if 0
|
||||
if (a2->type == QSE_AWK_VAL_MAP)
|
||||
{
|
||||
/* if the 3rd paramater is an array,
|
||||
* it is a placeholder to store parenthesized
|
||||
* subexpressions */
|
||||
|
||||
/* TODO: please implement this... */
|
||||
start = 0;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
/* if the 3rd parameter is not an array,
|
||||
* it is treated as a match start index */
|
||||
n = qse_awk_rtx_valtoint (rtx, a2, &start);
|
||||
if (n <= -1) return -1;
|
||||
}
|
||||
/* if the 3rd parameter is not an array,
|
||||
* it is treated as a match start index */
|
||||
n = qse_awk_rtx_valtoint (rtx, a2, &start);
|
||||
if (n <= -1) return -1;
|
||||
}
|
||||
|
||||
str0 = qse_awk_rtx_getvalstr (rtx, a0, &len0);
|
||||
@ -1210,6 +1188,7 @@ int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
|
||||
qse_awk_rtx_freevalstr (rtx, a0, str0);
|
||||
|
||||
/* RSTART: 0 on no match */
|
||||
idx = (n == 0)? 0: ((qse_awk_int_t)(mat.ptr-str0) + 1);
|
||||
|
||||
a0 = qse_awk_rtx_makeintval (rtx, idx);
|
||||
@ -1217,6 +1196,7 @@ int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
|
||||
qse_awk_rtx_refupval (rtx, a0);
|
||||
|
||||
/* RLENGTH: -1 on no match */
|
||||
a1 = qse_awk_rtx_makeintval (rtx,
|
||||
((n == 0)? (qse_awk_int_t)-1: (qse_awk_int_t)mat.len));
|
||||
if (a1 == QSE_NULL)
|
||||
@ -1241,6 +1221,8 @@ int qse_awk_fnc_match (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* TOOD: set RMATCH to hold the mainmatch and submatches inside () */
|
||||
|
||||
qse_awk_rtx_setretval (rtx, a0);
|
||||
|
||||
qse_awk_rtx_refdownval (rtx, a1);
|
||||
|
@ -4183,6 +4183,7 @@ static int __cmp_int_nil (
|
||||
static int __cmp_int_int (
|
||||
qse_awk_rtx_t* run, qse_awk_val_t* left, qse_awk_val_t* right)
|
||||
{
|
||||
|
||||
qse_awk_int_t v1 = qse_awk_rtx_getintfromval (rtx, left);
|
||||
qse_awk_int_t v2 = qse_awk_rtx_getintfromval (rtx, right);
|
||||
return (v1 > v2)? 1: ((v1 < v2)? -1: 0);
|
||||
@ -6078,7 +6079,7 @@ static qse_awk_val_t* eval_int (qse_awk_rtx_t* run, qse_awk_nde_t* nde)
|
||||
|
||||
val = qse_awk_rtx_makeintval (run, ((qse_awk_nde_int_t*)nde)->val);
|
||||
if (val == QSE_NULL) ADJERR_LOC (run, &nde->loc);
|
||||
else ((qse_awk_val_int_t*)val)->nde = nde;
|
||||
else if (IS_REAL_POINTER(val)) ((qse_awk_val_int_t*)val)->nde = nde;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
@ -33,37 +33,46 @@ static qse_awk_val_str_t awk_zls = { QSE_AWK_VAL_STR, 0, 1, 0, { QSE_T(""), 0 }
|
||||
qse_awk_val_t* qse_awk_val_nil = (qse_awk_val_t*)&awk_nil;
|
||||
qse_awk_val_t* qse_awk_val_zls = (qse_awk_val_t*)&awk_zls;
|
||||
|
||||
/*
|
||||
* qse_awk_makeintval() returns a quickint pointer
|
||||
* for the value of -1, 0, 1. these static pointers
|
||||
* are kind of unnecesary for the quickint potiners.
|
||||
* let me keep this table until i redefine
|
||||
* qse_awk_val_negone, qse_awk_val_zero, qse_awk_val_one
|
||||
* to proper quickint pointer values.
|
||||
*
|
||||
* #define qse_awk_val_zero (qse_awk_val_t*)((qse_uintptr)VAL_TYPE_BITS_QUICKINT)
|
||||
* #define qse_awk_val_one (qse_awk_val_t*)(((qse_uintptr)1 << NUM_TYPE_BITS) | VAL_TYPE_BITS_QUICKINT)
|
||||
* #define qse_awk_val_negone (qse_awk_val_t*)(((qse_uintptr)1 << NUM_TYPE_BITS) | VAL_TYPE_BITS_QUICKINT | VAL_SIGN_BIT)
|
||||
*/
|
||||
static qse_awk_val_int_t awk_int[] =
|
||||
{
|
||||
/* type ref stat nstr val nde */
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, -1, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 0, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 1, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 2, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 3, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 4, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 5, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 6, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 7, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 8, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 9, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 10, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 11, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 12, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 13, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 14, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 15, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 16, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 17, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 18, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 19, QSE_NULL },
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 20, QSE_NULL }
|
||||
{ QSE_AWK_VAL_INT, 0, 1, 0, 1, QSE_NULL }
|
||||
};
|
||||
|
||||
qse_awk_val_t* qse_awk_val_negone = (qse_awk_val_t*)&awk_int[0];
|
||||
qse_awk_val_t* qse_awk_val_zero = (qse_awk_val_t*)&awk_int[1];
|
||||
qse_awk_val_t* qse_awk_val_one = (qse_awk_val_t*)&awk_int[2];
|
||||
|
||||
static QSE_INLINE qse_awk_val_t* quickint_to_pointer (qse_awk_int_t v)
|
||||
{
|
||||
qse_uintptr_t r;
|
||||
|
||||
if (v < 0)
|
||||
{
|
||||
r = (((qse_uintptr_t)-v) << VAL_NUM_TYPE_BITS) | VAL_TYPE_BITS_QUICKINT | VAL_SIGN_BIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = ((qse_uintptr_t)v << VAL_NUM_TYPE_BITS) | VAL_TYPE_BITS_QUICKINT;
|
||||
}
|
||||
|
||||
return (qse_awk_val_t*)r;
|
||||
}
|
||||
|
||||
qse_awk_val_t* qse_getawknilval (void)
|
||||
{
|
||||
return (qse_awk_val_t*)&awk_nil;
|
||||
@ -79,51 +88,21 @@ qse_awk_val_t* qse_awk_rtx_makenilval (qse_awk_rtx_t* rtx)
|
||||
return (qse_awk_val_t*)&awk_nil;
|
||||
}
|
||||
|
||||
|
||||
#define NUM_TYPE_BITS 2
|
||||
#define TYPE_BITS_POINTER 0
|
||||
#define TYPE_BITS_QUICKINT 1
|
||||
#define TYPE_BITS_RESERVED_1 2
|
||||
#define TYPE_BITS_RESERVED_2 3
|
||||
#define SIGN_BIT ((qse_uintptr_t)1 << (QSE_SIZEOF(qse_uintptr_t) * 8 - 1))
|
||||
|
||||
/* shrink the bit range by 1 more bit to ease signbit handling.
|
||||
* i want abs(max) == abs(min).
|
||||
* i don't want abs(max) + 1 == abs(min). e.g min: -32768, max: 32767
|
||||
*/
|
||||
#define QUICKINT_MAX ((~(qse_uintptr_t)0) >> (NUM_TYPE_BITS + 1))
|
||||
#define QUICKINT_MIN (-QUICKINT_MAX)
|
||||
|
||||
#define IS_QUICKINT(v) ((v) >= QUICKINT_MIN && (v) <= QUICKINT_MAX)
|
||||
#define POINTER_TYPE_BITS(p) (((qse_uintptr_t)(p)) | NUM_TYPE_BITS)
|
||||
|
||||
static QSE_INLINE qse_awk_val_t* quickint_to_pointer (qse_awk_int_t v)
|
||||
{
|
||||
qse_uintptr_t r;
|
||||
|
||||
if (v < 0)
|
||||
{
|
||||
r = (((qse_uintptr_t)-v) << NUM_TYPE_BITS) | TYPE_BITS_QUICKINT | SIGN_BIT;
|
||||
}
|
||||
else
|
||||
{
|
||||
r = ((qse_uintptr_t)v << NUM_TYPE_BITS) | TYPE_BITS_QUICKINT;
|
||||
}
|
||||
|
||||
return (qse_awk_val_t*)r;
|
||||
}
|
||||
|
||||
qse_awk_val_t* qse_awk_rtx_makeintval (qse_awk_rtx_t* rtx, qse_awk_int_t v)
|
||||
{
|
||||
qse_awk_val_int_t* val;
|
||||
|
||||
// if (IS_QUICKINT(v)) return quickint_to_pinter (v);
|
||||
if (IS_QUICKINT(v)) return quickint_to_pointer(v);
|
||||
|
||||
#if 0
|
||||
/* this is not necesary as the numbers in awk_int are
|
||||
* covered by QUICKINTs */
|
||||
if (v >= awk_int[0].i_val &&
|
||||
v <= awk_int[QSE_COUNTOF(awk_int)-1].i_val)
|
||||
{
|
||||
return (qse_awk_val_t*)&awk_int[v-awk_int[0].i_val];
|
||||
}
|
||||
#endif
|
||||
|
||||
if (rtx->vmgr.ifree == QSE_NULL)
|
||||
{
|
||||
@ -788,176 +767,157 @@ qse_awk_val_t* qse_awk_rtx_makefunval (
|
||||
return (qse_awk_val_t*)val;
|
||||
}
|
||||
|
||||
/*
|
||||
* if shared objects link a static library, statically defined objects
|
||||
* in the static library will be instatiated in the multiple shared objects.
|
||||
*
|
||||
* so equality check with a value pointer doesn't work
|
||||
* if the code crosses the library boundaries. instead, i decided to
|
||||
* add a field to indicate if a value is static.
|
||||
*
|
||||
|
||||
#define IS_STATICVAL(val) \
|
||||
((val) == QSE_NULL || \
|
||||
(val) == qse_awk_val_nil || \
|
||||
(val) == qse_awk_val_zls || \
|
||||
(val) == qse_awk_val_zero || \
|
||||
(val) == qse_awk_val_one || \
|
||||
((val) >= (qse_awk_val_t*)&awk_int[0] && \
|
||||
(val) <= (qse_awk_val_t*)&awk_int[QSE_COUNTOF(awk_int)-1]))
|
||||
*/
|
||||
#define IS_STATICVAL(val) ((val)->stat)
|
||||
|
||||
int qse_awk_rtx_isstaticval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
||||
int QSE_INLINE qse_awk_rtx_isstaticval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
||||
{
|
||||
return IS_STATICVAL(val);
|
||||
return IS_REAL_POINTER(val) && IS_STATICVAL(val);
|
||||
}
|
||||
|
||||
void qse_awk_rtx_freeval (qse_awk_rtx_t* rtx, qse_awk_val_t* val, int cache)
|
||||
{
|
||||
qse_awk_val_type_t vtype;
|
||||
|
||||
switch (POINTER_TYPE_BITS(val))
|
||||
if (IS_REAL_POINTER(val))
|
||||
{
|
||||
// case TYPE_BITS_QUICKINT:
|
||||
/* do nothing */
|
||||
// return;
|
||||
if (IS_STATICVAL(val)) return;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#ifdef DEBUG_VAL
|
||||
qse_errputstrf (QSE_T("freeing [cache=%d] ... "), cache);
|
||||
qse_awk_dprintval (rtx, val);
|
||||
qse_errputstrf (QSE_T("\n"));
|
||||
#endif
|
||||
|
||||
if (IS_STATICVAL(val)) return;
|
||||
|
||||
#ifdef DEBUG_VAL
|
||||
qse_errputstrf (QSE_T("freeing [cache=%d] ... "), cache);
|
||||
qse_awk_dprintval (rtx, val);
|
||||
qse_errputstrf (QSE_T("\n"));
|
||||
#endif
|
||||
|
||||
vtype = qse_awk_rtx_getvaltype (rtx, val);
|
||||
switch (vtype)
|
||||
{
|
||||
case QSE_AWK_VAL_NIL:
|
||||
vtype = qse_awk_rtx_getvaltype (rtx, val);
|
||||
switch (vtype)
|
||||
{
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_INT:
|
||||
{
|
||||
((qse_awk_val_int_t*)val)->nde =
|
||||
(qse_awk_nde_int_t*)rtx->vmgr.ifree;
|
||||
rtx->vmgr.ifree = (qse_awk_val_int_t*)val;
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_FLT:
|
||||
{
|
||||
((qse_awk_val_flt_t*)val)->nde =
|
||||
(qse_awk_nde_flt_t*)rtx->vmgr.rfree;
|
||||
rtx->vmgr.rfree = (qse_awk_val_flt_t*)val;
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_STR:
|
||||
{
|
||||
#ifdef ENABLE_FEATURE_SCACHE
|
||||
if (cache)
|
||||
case QSE_AWK_VAL_NIL:
|
||||
{
|
||||
qse_awk_val_str_t* v = (qse_awk_val_str_t*)val;
|
||||
int i;
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
|
||||
i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT;
|
||||
if (i < QSE_COUNTOF(rtx->scache_count) &&
|
||||
rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i]))
|
||||
case QSE_AWK_VAL_INT:
|
||||
{
|
||||
((qse_awk_val_int_t*)val)->nde = (qse_awk_nde_int_t*)rtx->vmgr.ifree;
|
||||
rtx->vmgr.ifree = (qse_awk_val_int_t*)val;
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_FLT:
|
||||
{
|
||||
((qse_awk_val_flt_t*)val)->nde = (qse_awk_nde_flt_t*)rtx->vmgr.rfree;
|
||||
rtx->vmgr.rfree = (qse_awk_val_flt_t*)val;
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_STR:
|
||||
{
|
||||
#ifdef ENABLE_FEATURE_SCACHE
|
||||
if (cache)
|
||||
{
|
||||
rtx->scache[i][rtx->scache_count[i]++] = v;
|
||||
v->nstr = 0;
|
||||
qse_awk_val_str_t* v = (qse_awk_val_str_t*)val;
|
||||
int i;
|
||||
|
||||
i = v->val.len / FEATURE_SCACHE_BLOCK_UNIT;
|
||||
if (i < QSE_COUNTOF(rtx->scache_count) &&
|
||||
rtx->scache_count[i] < QSE_COUNTOF(rtx->scache[i]))
|
||||
{
|
||||
rtx->scache[i][rtx->scache_count[i]++] = v;
|
||||
v->nstr = 0;
|
||||
}
|
||||
else QSE_AWK_FREE (rtx->awk, val);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_REX:
|
||||
{
|
||||
/* don't free ptr as it is inlined to val
|
||||
QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr);
|
||||
*/
|
||||
|
||||
/* code is just a pointer to a regular expression stored
|
||||
* in parse tree nodes. so don't free it.
|
||||
qse_awk_freerex (rtx->awk, ((qse_awk_val_rex_t*)val)->code[0], ((qse_awk_val_rex_t*)val)->code[1]);
|
||||
*/
|
||||
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_MAP:
|
||||
{
|
||||
qse_htb_fini (((qse_awk_val_map_t*)val)->map);
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_REF:
|
||||
{
|
||||
if (cache && rtx->rcache_count < QSE_COUNTOF(rtx->rcache))
|
||||
{
|
||||
rtx->rcache[rtx->rcache_count++] = (qse_awk_val_ref_t*)val;
|
||||
}
|
||||
else QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_REX:
|
||||
{
|
||||
/* don't free ptr as it is inlined to val
|
||||
QSE_AWK_FREE (rtx->awk, ((qse_awk_val_rex_t*)val)->ptr);
|
||||
*/
|
||||
|
||||
/* code is just a pointer to a regular expression stored
|
||||
* in parse tree nodes. so don't free it.
|
||||
qse_awk_freerex (rtx->awk, ((qse_awk_val_rex_t*)val)->code[0], ((qse_awk_val_rex_t*)val)->code[1]);
|
||||
*/
|
||||
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_MAP:
|
||||
{
|
||||
qse_htb_fini (((qse_awk_val_map_t*)val)->map);
|
||||
QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
|
||||
case QSE_AWK_VAL_REF:
|
||||
{
|
||||
if (cache && rtx->rcache_count < QSE_COUNTOF(rtx->rcache))
|
||||
{
|
||||
rtx->rcache[rtx->rcache_count++] = (qse_awk_val_ref_t*)val;
|
||||
}
|
||||
else QSE_AWK_FREE (rtx->awk, val);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void qse_awk_rtx_refupval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
||||
{
|
||||
if (IS_STATICVAL(val)) return;
|
||||
if (IS_REAL_POINTER(val))
|
||||
{
|
||||
if (IS_STATICVAL(val)) return;
|
||||
|
||||
#ifdef DEBUG_VAL
|
||||
qse_errputstrf (QSE_T("ref up [ptr=%p] [count=%d] "), val, (int)val->ref);
|
||||
qse_awk_dprintval (rtx, val);
|
||||
qse_errputstrf (QSE_T("\n"));
|
||||
#endif
|
||||
|
||||
val->ref++;
|
||||
#ifdef DEBUG_VAL
|
||||
qse_errputstrf (QSE_T("ref up [ptr=%p] [count=%d] "), val, (int)val->ref);
|
||||
qse_awk_dprintval (rtx, val);
|
||||
qse_errputstrf (QSE_T("\n"));
|
||||
#endif
|
||||
val->ref++;
|
||||
}
|
||||
}
|
||||
|
||||
void qse_awk_rtx_refdownval (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
||||
{
|
||||
if (IS_STATICVAL(val)) return;
|
||||
|
||||
#ifdef DEBUG_VAL
|
||||
qse_errputstrf (QSE_T("ref down [ptr=%p] [count=%d]\n"), val, (int)val->ref);
|
||||
qse_awk_dprintval (rtx, val);
|
||||
qse_errputstrf (QSE_T("\n"));
|
||||
#endif
|
||||
|
||||
QSE_ASSERTX (val->ref > 0,
|
||||
"the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs");
|
||||
|
||||
val->ref--;
|
||||
if (val->ref <= 0)
|
||||
if (IS_REAL_POINTER(val))
|
||||
{
|
||||
qse_awk_rtx_freeval(rtx, val, 1);
|
||||
if (IS_STATICVAL(val)) return;
|
||||
|
||||
#ifdef DEBUG_VAL
|
||||
qse_errputstrf (QSE_T("ref down [ptr=%p] [count=%d]\n"), val, (int)val->ref);
|
||||
qse_awk_dprintval (rtx, val);
|
||||
qse_errputstrf (QSE_T("\n"));
|
||||
#endif
|
||||
|
||||
QSE_ASSERTX (val->ref > 0,
|
||||
"the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs");
|
||||
|
||||
val->ref--;
|
||||
if (val->ref <= 0)
|
||||
{
|
||||
qse_awk_rtx_freeval(rtx, val, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void qse_awk_rtx_refdownval_nofree (qse_awk_rtx_t* rtx, qse_awk_val_t* val)
|
||||
{
|
||||
if (IS_STATICVAL(val)) return;
|
||||
if (IS_REAL_POINTER(val))
|
||||
{
|
||||
if (IS_STATICVAL(val)) return;
|
||||
|
||||
QSE_ASSERTX (val->ref > 0,
|
||||
"the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs");
|
||||
val->ref--;
|
||||
QSE_ASSERTX (val->ref > 0,
|
||||
"the reference count of a value should be greater than zero for it to be decremented. check the source code for any bugs");
|
||||
|
||||
val->ref--;
|
||||
}
|
||||
}
|
||||
|
||||
void qse_awk_rtx_freevalchunk (qse_awk_rtx_t* rtx, qse_awk_val_chunk_t* chunk)
|
||||
@ -970,8 +930,7 @@ void qse_awk_rtx_freevalchunk (qse_awk_rtx_t* rtx, qse_awk_val_chunk_t* chunk)
|
||||
}
|
||||
}
|
||||
|
||||
static int val_ref_to_bool (
|
||||
qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref)
|
||||
static int val_ref_to_bool (qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref)
|
||||
{
|
||||
switch (ref->id)
|
||||
{
|
||||
@ -1559,14 +1518,13 @@ qse_char_t* qse_awk_rtx_getvalstr (
|
||||
void qse_awk_rtx_freevalstr (
|
||||
qse_awk_rtx_t* rtx, const qse_awk_val_t* v, qse_char_t* str)
|
||||
{
|
||||
if (qse_awk_rtx_getvaltype(rtx, v) != QSE_AWK_VAL_STR &&
|
||||
if (qse_awk_rtx_getvaltype(rtx, v) != QSE_AWK_VAL_STR ||
|
||||
str != ((qse_awk_val_str_t*)v)->val.ptr)
|
||||
{
|
||||
qse_awk_rtx_freemem (rtx, str);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int val_ref_to_num (
|
||||
qse_awk_rtx_t* rtx, const qse_awk_val_ref_t* ref, qse_awk_int_t* l, qse_awk_flt_t* r)
|
||||
{
|
||||
|
@ -50,6 +50,61 @@ struct qse_awk_val_rchunk_t
|
||||
qse_awk_val_flt_t slot[QSE_AWK_VAL_CHUNK_SIZE];
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* if shared objects link a static library, statically defined objects
|
||||
* in the static library will be instatiated in the multiple shared objects.
|
||||
*
|
||||
* so equality check with a value pointer doesn't work
|
||||
* if the code crosses the library boundaries. instead, i decided to
|
||||
* add a field to indicate if a value is static.
|
||||
*
|
||||
|
||||
#define IS_STATICVAL(val) \
|
||||
((val) == QSE_NULL || \
|
||||
(val) == qse_awk_val_nil || \
|
||||
(val) == qse_awk_val_zls || \
|
||||
(val) == qse_awk_val_zero || \
|
||||
(val) == qse_awk_val_one || \
|
||||
((val) >= (qse_awk_val_t*)&awk_int[0] && \
|
||||
(val) <= (qse_awk_val_t*)&awk_int[QSE_COUNTOF(awk_int)-1]))
|
||||
*/
|
||||
#define IS_STATICVAL(val) ((val)->stat)
|
||||
|
||||
|
||||
|
||||
/* qse_awk_val_t pointer encoding assumes that
|
||||
* the 2 least significant bits of a real pointer are all zeros. */
|
||||
#define VAL_NUM_TYPE_BITS 2
|
||||
#define VAL_MASK_TYPE_BITS 3
|
||||
|
||||
#define VAL_TYPE_BITS_POINTER 0
|
||||
#define VAL_TYPE_BITS_QUICKINT 1
|
||||
#define VAL_TYPE_BITS_RESERVED_1 2
|
||||
#define VAL_TYPE_BITS_RESERVED_2 3
|
||||
#define VAL_SIGN_BIT ((qse_uintptr_t)1 << (QSE_SIZEOF_UINTPTR_T * 8 - 1))
|
||||
|
||||
/* shrink the bit range by 1 more bit to ease signbit handling.
|
||||
* i want abs(max) == abs(min).
|
||||
* i don't want abs(max) + 1 == abs(min). e.g min: -32768, max: 32767
|
||||
*/
|
||||
#define QUICKINT_MAX ((qse_awk_int_t)((~(qse_uintptr_t)0) >> (VAL_NUM_TYPE_BITS + 1)))
|
||||
#define QUICKINT_MIN (-QUICKINT_MAX)
|
||||
|
||||
#define IS_QUICKINT(i) ((i) >= QUICKINT_MIN && (i) <= QUICKINT_MAX)
|
||||
|
||||
#define POINTER_TYPE_BITS(p) (((qse_uintptr_t)(p)) & VAL_MASK_TYPE_BITS)
|
||||
#define IS_REAL_POINTER(p) (POINTER_TYPE_BITS(p) == VAL_TYPE_BITS_POINTER)
|
||||
#define IS_QUICKINT_POINTER(p) (POINTER_TYPE_BITS(p) == VAL_TYPE_BITS_QUICKINT)
|
||||
|
||||
/* sizeof(qse_intptr_t) may not be the same as sizeof(qse_awk_int_t).
|
||||
* so step-by-step type conversions are needed.
|
||||
* e.g) pointer to uintptr_t, uintptr_t to intptr_t, intptr_t to awk_int_t */
|
||||
#define GET_QUICKINT_FROM_POINTER(p) (((qse_uintptr_t)(p) & VAL_SIGN_BIT)? -(qse_intptr_t)(((qse_uintptr_t)(p) & ~VAL_SIGN_BIT) >> 2): (qse_intptr_t)((qse_uintptr_t)(p) >> 2))
|
||||
|
||||
#define qse_awk_rtx_getvaltype(rtx,p) (IS_QUICKINT_POINTER(p)? QSE_AWK_VAL_INT: (p)->v_type)
|
||||
#define qse_awk_rtx_getintfromval(rtx,p) ((IS_QUICKINT_POINTER(p)? (qse_awk_int_t)GET_QUICKINT_FROM_POINTER(p): ((qse_awk_val_int_t*)(p))->i_val))
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user