Skip to content

Commit

Permalink
Changed intptr_union to struct type, added shared pointer to struct
Browse files Browse the repository at this point in the history
Changing `intptr_union` to a struct was necessary as `std::shared_ptr` cannot be
constructed/destructed in unions.

This change will not leak any memory, but now `intptr_union` is now larger so parsing will take up
more memory.
  • Loading branch information
Nydauron committed Apr 24, 2023
1 parent 155e9ab commit 1d6fa3c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
69 changes: 24 additions & 45 deletions spim/CPU/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -533,17 +533,15 @@ LBL_CMD: OPT_LBL CMD
OPT_LBL: ID ':' {
/* Call outside of cons_label, since an error sets that variable to NULL. */
label* l = record_label (img,
(char*)$1.p,
$1.s.get(),
text_dir ? current_text_pc (img) : current_data_pc (img),
0);
this_line_labels = cons_label (l, this_line_labels);
free ((char*)$1.p);
}

| ID '=' EXPR
{
label *l = record_label (img, (char*)$1.p, (mem_addr)$3.i, 1);
free ((char*)$1.p);
label *l = record_label (img, $1.s.get(), (mem_addr)$3.i, 1);

l->const_flag = 1;
clear_labels (img);
Expand Down Expand Up @@ -2173,10 +2171,9 @@ ASM_DIRECTIVE: Y_ALIAS_DIR Y_REG Y_REG
| Y_COMM_DIR ID EXPR
{
align_data (img, 2);
if (lookup_label (img, (char*)$2.p)->addr == 0)
if (lookup_label (img, $2.s.get())->addr == 0)
{
(void)record_label (img, (char*)$2.p, current_data_pc (img), 1);
free ((char*)$2.p);
(void)record_label (img, $2.s.get(), current_data_pc (img), 1);
}
increment_data_pc (img, $3.i);
}
Expand Down Expand Up @@ -2238,8 +2235,7 @@ ASM_DIRECTIVE: Y_ALIAS_DIR Y_REG Y_REG

| Y_EXTERN_DIR ID EXPR
{
extern_directive (img, (char*)$2.p, $3.i);
free((char *) $2.p);
extern_directive (img, $2.s.get(), $3.i);
}


Expand Down Expand Up @@ -2271,8 +2267,7 @@ ASM_DIRECTIVE: Y_ALIAS_DIR Y_REG Y_REG

| Y_GLOBAL_DIR ID
{
(void)make_label_global (img, (char*)$2.p);
free ((char*)$2.p);
(void)make_label_global (img, $2.s.get());
}


Expand All @@ -2291,16 +2286,15 @@ ASM_DIRECTIVE: Y_ALIAS_DIR Y_REG Y_REG
| Y_LABEL_DIR ID
{
(void)record_label (img,
(char*)$2.p,
$2.s.get(),
text_dir ? current_text_pc (img) : current_data_pc (img),
1);
free ((char*)$2.p);
}


| Y_LCOMM_DIR ID EXPR
{
lcomm_directive (img, (char*)$2.p, $3.i);
lcomm_directive (img, $2.s.get(), $3.i);
free((char *) $2.p);
}

Expand Down Expand Up @@ -2357,11 +2351,10 @@ ASM_DIRECTIVE: Y_ALIAS_DIR Y_REG Y_REG

| Y_SET_DIR ID
{
if (streq ((char*)$2.p, "noat"))
if (streq ($2.s.get(), "noat"))
noat_flag = true;
else if (streq ((char*)$2.p, "at"))
else if (streq ($2.s.get(), "at"))
noat_flag = false;
free((char*) $2.p);
}


Expand Down Expand Up @@ -2447,44 +2440,37 @@ ADDR: '(' REGISTER ')'

| Y_ID
{
$$.p = make_addr_expr (img, 0, (char*)$1.p, 0);
free ((char*)$1.p);
$$.p = make_addr_expr (img, 0, $1.s.get(), 0);
}

| Y_ID '(' REGISTER ')'
{
$$.p = make_addr_expr (img, 0, (char*)$1.p, $3.i);
free ((char*)$1.p);
$$.p = make_addr_expr (img, 0, $1.s.get(), $3.i);
}

| Y_ID '+' ABS_ADDR
{
$$.p = make_addr_expr (img, $3.i, (char*)$1.p, 0);
free ((char*)$1.p);
$$.p = make_addr_expr (img, $3.i, $1.s.get(), 0);
}

| ABS_ADDR '+' ID
{
$$.p = make_addr_expr (img, $1.i, (char*)$3.p, 0);
free((char *) $3.p);
$$.p = make_addr_expr (img, $1.i, $3.s.get(), 0);
}

| Y_ID '-' ABS_ADDR
{
$$.p = make_addr_expr (img, - $3.i, (char*)$1.p, 0);
free ((char*)$1.p);
$$.p = make_addr_expr (img, - $3.i, $1.s.get(), 0);
}

| Y_ID '+' ABS_ADDR '(' REGISTER ')'
{
$$.p = make_addr_expr (img, $3.i, (char*)$1.p, $5.i);
free ((char*)$1.p);
$$.p = make_addr_expr (img, $3.i, $1.s.get(), $5.i);
}

| Y_ID '-' ABS_ADDR '(' REGISTER ')'
{
$$.p = make_addr_expr (img, - $3.i, (char*)$1.p, $5.i);
free ((char*)$1.p);
$$.p = make_addr_expr (img, - $3.i, $1.s.get(), $5.i);
}
;

Expand Down Expand Up @@ -2516,20 +2502,17 @@ IMM32: ABS_ADDR

| ID
{
$$.p = make_imm_expr (img, 0, (char*)$1.p, false);
free((char *) $1.p);
$$.p = make_imm_expr (img, 0, $1.s.get(), false);
}

| Y_ID '+' ABS_ADDR
{
$$.p = make_imm_expr (img, $3.i, (char*)$1.p, false);
free ((char*)$1.p);
$$.p = make_imm_expr (img, $3.i, $1.s.get(), false);
}

| Y_ID '-' ABS_ADDR
{
$$.p = make_imm_expr (img, - $3.i, (char*)$1.p, false);
free ((char*)$1.p);
$$.p = make_imm_expr (img, - $3.i, $1.s.get(), false);
}
;

Expand Down Expand Up @@ -2597,8 +2580,7 @@ COP_REG: Y_REG

LABEL: ID
{
$$.p = make_imm_expr (img, -(int)current_text_pc (img), (char*)$1.p, true);
free((char *)$1.p);
$$.p = make_imm_expr (img, -(int)current_text_pc (img), $1.s.get(), true);
}


Expand All @@ -2609,16 +2591,14 @@ STR_LST: STR_LST STR

STR: Y_STR
{
store_string (img, (char*)$1.p, strlen((char*)$1.p), null_term);
free ((char*)$1.p);
store_string (img, $1.s.get(), strlen($1.s.get()), null_term);
}
| Y_STR ':' Y_INT
{
int i;

for (i = 0; i < $3.i; i ++)
store_string (img, (char*)$1.p, strlen((char*)$1.p), null_term);
free ((char*)$1.p);
store_string (img, $1.s.get(), strlen($1.s.get()), null_term);
}
;

Expand Down Expand Up @@ -2652,8 +2632,7 @@ FACTOR: Y_INT

| ID
{
label *l = lookup_label (img, (char*)$1.p);
free((char *) $1.p);
label *l = lookup_label (img, $1.s.get());
if (l->addr == 0)
{
record_data_uses_symbol (img, current_data_pc (img), l);
Expand Down
8 changes: 4 additions & 4 deletions spim/CPU/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static char scan_escape (MIPSImage &img, char **str);
else
{
/* Not-yet defined label */
yylval.p = (char*) str_copy (img, yytext);
yylval.s = std::shared_ptr<char>((char*) str_copy (img, yytext), [] (auto p) { free(p); });
return (Y_ID);
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ static char scan_escape (MIPSImage &img, char **str);
}
else
{
yylval.p = (char*) str_copy (img, yytext);
yylval.s = std::shared_ptr<char>((char*) str_copy (img, yytext), [] (auto p) { free(p); });
return (Y_ID);
}
}
Expand Down Expand Up @@ -270,7 +270,7 @@ static char scan_escape (MIPSImage &img, char **str);
current_line_no = line_no;
current_line = yytext;
}
yylval.p = (char*) str_copy (img, yytext);
yylval.s = std::shared_ptr<char>((char*) str_copy (img, yytext), [] (auto p) { free(p); });
/* For top level */
return (Y_ID);
}
Expand All @@ -282,7 +282,7 @@ static char scan_escape (MIPSImage &img, char **str);
current_line_no = line_no;
current_line = yytext;
}
yylval.p = (char*) copy_str (img, yytext + 1, 1);
yylval.s = std::shared_ptr<char>((char*) copy_str (img, yytext + 1, 1), [] (auto p) { free(p); });
return (Y_STR);
}

Expand Down
9 changes: 8 additions & 1 deletion spim/CPU/types.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#pragma once

#include <memory>

/* Type declarations for portability. They work for DEC's Alpha (64 bits)
and 32 bit machines */

typedef int int32;
typedef unsigned int uint32;
typedef union {int i; void* p;} intptr_union;
typedef struct {
int i;
void *p;
std::shared_ptr<char> s;
} intptr_union;

0 comments on commit 1d6fa3c

Please sign in to comment.