Skip to content

Commit

Permalink
[imcc] the plot thickens. pass the ghash|0 to _mk_const
Browse files Browse the repository at this point in the history
do not unescape binary strings containing 0
  • Loading branch information
Reini Urban committed Oct 15, 2014
1 parent df23fff commit dc829f1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 40 deletions.
14 changes: 7 additions & 7 deletions compilers/imcc/optimizer.c
Expand Up @@ -649,7 +649,7 @@ strength_reduce(ARGMOD(imc_info_t *imcc), ARGMOD(IMC_Unit *unit))
IMCC_debug_ins(imcc, DEBUG_OPT1, ins);
IMCC_debug(imcc, DEBUG_OPT1, " => ");
}
r = mk_const(imcc, "0", ins->symregs[0]->set);
r = _mk_const(imcc, 0, "0", ins->symregs[0]->set);
--ins->symregs[1]->use_count;
if (ins->opsize == 4)
--ins->symregs[2]->use_count;
Expand Down Expand Up @@ -727,7 +727,7 @@ constant_propagation(ARGMOD(imc_info_t *imcc), ARGMOD(IMC_Unit *unit))
}
else if (STREQ(ins->opname, "null") && ins->symregs[0]->set == 'I') {
found = 1;
c = mk_const(imcc, "0", 'I');
c = _mk_const(imcc, 0, "0", 'I');
o = ins->symregs[0];
} /* this would be good because 'set I0, 0' is reduced to 'null I0'
before it gets to us */
Expand Down Expand Up @@ -860,7 +860,7 @@ IMCC_subst_constants_umix(ARGMOD(imc_info_t *imcc), ARGMOD(IMC_Unit *unit),
STREQ(name, ops[i])) {
IMCC_debug(imcc, DEBUG_OPT1, "opt1 %s_nc_ic => ", name);
strcpy(b, r[1]->name);
r[1] = mk_const(imcc, b, 'N');
r[1] = _mk_const(imcc, 0, b, 'N');
tmp = INS(imcc, unit, name, "", r, 2, 0, 0);
IMCC_debug_ins(imcc, DEBUG_OPT1, tmp);
}
Expand Down Expand Up @@ -1116,22 +1116,22 @@ IMCC_subst_constants(ARGMOD(imc_info_t *imcc), ARGMOD(IMC_Unit *unit),
switch (r[0]->set) {
case 'I':
snprintf(b, sizeof (b), INTVAL_FMT, REG_INT(imcc->interp, 0));
r[1] = mk_const(imcc, b, r[0]->set);
r[1] = _mk_const(imcc, 0, b, r[0]->set);
break;
case 'N':
snprintf(b, sizeof (b), fmt, REG_NUM(imcc->interp, 0));
r[1] = mk_const(imcc, b, r[0]->set);
r[1] = _mk_const(imcc, 0, b, r[0]->set);
break;
case 'S':
{
char * const cstr = Parrot_str_to_cstring(imcc->interp, REG_STR(imcc->interp, 0));
const STR_VTABLE* encoding = REG_STR(imcc->interp, 0)->encoding;
if (encoding == Parrot_ascii_encoding_ptr) {
r[1] = mk_const(imcc, cstr, r[0]->set);
r[1] = _mk_const(imcc, 0, cstr, r[0]->set);
}
else {
snprintf(b, sizeof (b), "%s:\"%s\"", encoding->name, cstr);
r[1] = mk_const(imcc, b, 'U');
r[1] = _mk_const(imcc, 0, b, 'U');
}
Parrot_str_free_cstring(cstr);
}
Expand Down
4 changes: 2 additions & 2 deletions compilers/imcc/parser_util.c
Expand Up @@ -263,7 +263,7 @@ var_arg_ins(ARGMOD(imc_info_t * imcc), ARGMOD(IMC_Unit *unit), ARGIN(const char
"The opcode '%s' needs arguments", name);

if (r[0]->set == 'S') {
r[0] = mk_const(imcc, r[0]->name, 'P');
r[0] = _mk_const(imcc, 0, r[0]->name, 'P');
r[0]->pmc_type = enum_class_FixedIntegerArray;
}

Expand Down Expand Up @@ -520,7 +520,7 @@ change_op_arg_to_num(ARGMOD(imc_info_t * imcc), ARGMOD(IMC_Unit *unit),
if (c->type & VT_CONSTP)
c = c->reg;

r[num] = mk_const(imcc, c->name, 'N');
r[num] = _mk_const(imcc, 0, c->name, 'N');
changed = 1;
}
else if (emit) {
Expand Down
19 changes: 1 addition & 18 deletions compilers/imcc/pbc.c
Expand Up @@ -985,7 +985,7 @@ fixup_globals(ARGMOD(imc_info_t * imcc))
}
}
if (!s1) {
SymReg * const nam = mk_const(imcc, fixup->name,
SymReg * const nam = _mk_const(imcc, 0, fixup->name,
fixup->type & VT_ENCODED ? 'U' : 'S');

/* TODO: Don't hard-code this op name in here. Ask libparrot
Expand Down Expand Up @@ -1072,23 +1072,6 @@ IMCC_string_from_reg(ARGMOD(imc_info_t * imcc), ARGIN(SymReg *r))

return Parrot_str_unescape(imcc->interp, p+1, '"', encoding_name);
}
#if 0
/* mk_const already stripped the quotes.
don't strip valid quotes from the string again. t/op/basic_6.pasm */
else if (*buf == '"') {
buf++;
return *buf
? Parrot_str_unescape(imcc->interp, buf, '"', NULL)
: STRINGNULL; //Parrot_str_new_init(imcc->interp, "", 0,
//Parrot_ascii_encoding_ptr, PObj_constant_FLAG);
}
else if (*buf == '\'') {
buf++;
return *buf ? Parrot_str_new_init(imcc->interp, buf, strlen(buf) - 1,
Parrot_ascii_encoding_ptr, PObj_constant_FLAG)
: STRINGNULL;
}
#endif
bare:
/* unquoted bare name - ASCII only don't unescape it */
return *buf ? Parrot_str_new_init(imcc->interp, buf, strlen(buf),
Expand Down
8 changes: 4 additions & 4 deletions compilers/imcc/pcc.c
Expand Up @@ -185,7 +185,7 @@ get_const(ARGMOD(imc_info_t * imcc), ARGIN(const char *name), int type)
if (r && r->set == type)
return r;

return mk_const(imcc, name, type);
return _mk_const(imcc, 0, name, type);
}

/*
Expand Down Expand Up @@ -317,7 +317,7 @@ pcc_get_args(ARGMOD(imc_info_t * imcc), ARGMOD(IMC_Unit *unit),
bufpos--;
memcpy(buf + bufpos, subf, lensubf);

regs[0] = mk_const(imcc, buf, 'P');
regs[0] = _mk_const(imcc, 0, buf, 'P');
regs[0]->pmc_type = enum_class_FixedIntegerArray;
ins = insINS(imcc, unit, ins, op_name, regs, n + 1);

Expand Down Expand Up @@ -638,10 +638,10 @@ expand_pcc_sub_call(ARGMOD(imc_info_t * imcc), ARGMOD(IMC_Unit *unit),
|| arg->type == VTPASM
|| arg->type == VTREG)) {
if (arg->type & VT_ENCODED) {
meth = mk_const(imcc, arg->name, 'U');
meth = _mk_const(imcc, 0, arg->name, 'U');
}
else {
meth = mk_const(imcc, arg->name, 'S');
meth = _mk_const(imcc, 0, arg->name, 'S');
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions compilers/imcc/symreg.c
Expand Up @@ -749,7 +749,11 @@ mk_const_ident(ARGMOD(imc_info_t * imcc), ARGIN(const char *name), int t,
=item C<SymReg * _mk_const(imc_info_t * imcc, SymHash *hsh, const char *name,
int t)>
Makes a new constant (internal use only).
Makes a new constant without stripping surrounding string quotes.
Must be used internally.
If hsh is 0, the global symbol table is used.
mk_const which strips the quotes is only to be used from the parser.
=cut
Expand All @@ -758,11 +762,11 @@ Makes a new constant (internal use only).
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
SymReg *
_mk_const(ARGMOD(imc_info_t * imcc), ARGMOD(SymHash *hsh),
ARGIN(const char *name), int t)
_mk_const(ARGMOD(imc_info_t * imcc), ARGMOD_NULLOK(SymHash *hsh),
ARGIN(const char *name), int t)
{
ASSERT_ARGS(_mk_const)
SymReg * const r = _mk_symreg(imcc, hsh, name, t);
SymReg * const r = _mk_symreg(imcc, hsh ? hsh : &imcc->ghash, name, t);
r->type = VTCONST;

if (t == 'U') {
Expand Down Expand Up @@ -834,6 +838,8 @@ int_overflows(ARGIN(const SymReg *r))
=item C<SymReg * mk_const(imc_info_t * imcc, const char *name, int t)>
Makes a new constant and populates the cache of global symbols.
Strips surrounding string quotes and unescapes double-quoted strings.
Must be used in the parser.
=cut
Expand All @@ -854,15 +860,17 @@ mk_const(ARGMOD(imc_info_t * imcc), ARGIN(const char *name), int t)
create_symhash(imcc, h);

if (t != 'U') {
if (*name == '"') {
if (*name == '"') { /* but we need to keep escaped \0 */
STRING *unescaped = Parrot_str_unescape(imcc->interp, name+1, '"', NULL);
const_name = Parrot_str_to_cstring(imcc->interp, unescaped);
if (!memchr(unescaped->strstart, 0, unescaped->bufused))
const_name = Parrot_str_to_cstring(imcc->interp, unescaped);
}
else if (*name == '\'') {
const_name = mem_sys_strdup(name + 1);
const_name[strlen(const_name) - 1] = 0;
}
}
/* TODO: resolve encoding aliases here with U */

IMCC_debug(imcc, DEBUG_MKCONST, "# mk_const '%s' %c\n",
const_name, t);
Expand Down
4 changes: 1 addition & 3 deletions compilers/imcc/symreg.h
Expand Up @@ -165,11 +165,10 @@ PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
SymReg * _mk_const(
ARGMOD(imc_info_t * imcc),
ARGMOD(SymHash *hsh),
ARGMOD_NULLOK(SymHash *hsh),
ARGIN(const char *name),
int t)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(* imcc)
FUNC_MODIFIES(*hsh);
Expand Down Expand Up @@ -422,7 +421,6 @@ char * symreg_to_str(ARGIN(const SymReg *s))
, PARROT_ASSERT_ARG(name))
#define ASSERT_ARGS__mk_const __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(imcc) \
, PARROT_ASSERT_ARG(hsh) \
, PARROT_ASSERT_ARG(name))
#define ASSERT_ARGS__store_symreg __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(imcc) \
Expand Down

0 comments on commit dc829f1

Please sign in to comment.