Skip to content

Commit

Permalink
feat(structs): fix segfault when using structs in boolean expression, c…
Browse files Browse the repository at this point in the history
…loses #1664
  • Loading branch information
hlolli committed Dec 13, 2022
1 parent c02b518 commit 58aa15b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
22 changes: 16 additions & 6 deletions Engine/csound_orc_expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ int is_expression_node(TREE *node)
case '^':
case T_FUNCTION:
case S_UMINUS:
case S_UPLUS:
case S_UPLUS:
case '|':
case '&':
case S_BITSHIFT_RIGHT:
Expand Down Expand Up @@ -692,7 +692,7 @@ static TREE *create_expression(CSOUND *csound, TREE *root, int line, int locn,
root->right, typeTable);

break;

case '|':
strNcpy(op, "##or", 80);
outarg = create_out_arg_for_expression(csound, op, root->left,
Expand Down Expand Up @@ -771,7 +771,7 @@ static TREE *create_expression(CSOUND *csound, TREE *root, int line, int locn,
outype = strdup(var->subType->varTypeName);
/* VL: 9.2.22 pulled code from 6.x to check for array index type
to provide the correct outype. Works with explicity types
*/
*/
if (outype[0]== 'i') {
TREE* inds = root->right;
while (inds) {
Expand Down Expand Up @@ -969,14 +969,24 @@ static TREE *create_boolean_expression(CSOUND *csound, TREE *root,
typeTable,
argtyp2( root->left->value->lexeme) =='k' ||
argtyp2( root->left->value->lexeme) =='B');
}
else
} else if (root->left->type == STRUCT_EXPR) {
char* structArgType = get_arg_type2(csound, root->left, typeTable);
if (strlen(structArgType) > 1) {
synterr(csound,
Str("invalid use of struct in boolean expression %s line %d\n"),
structArgType, root->left->line);
return NULL;
}
outarg = get_boolean_arg(csound, typeTable, *structArgType);
} else {
outarg = get_boolean_arg(csound,
typeTable,
argtyp2( root->left->value->lexeme) =='k' ||
argtyp2( root->right->value->lexeme)=='k' ||
argtyp2( root->left->value->lexeme) =='B' ||
argtyp2( root->right->value->lexeme)=='B');
}


add_arg(csound, outarg, NULL, typeTable);
opTree = create_opcode_token(csound, op);
Expand Down Expand Up @@ -1434,7 +1444,7 @@ TREE* expand_until_statement(CSOUND* csound, TREE* current,
gotoType =
last->left->value->lexeme[1] == 'B'; // checking for #B... var name

// printf("%s\n", last->left->value->lexeme);
// printf("%s\n", last->left->value->lexeme);
//printf("gottype = %d ; dowhile = %d\n", gotoType, dowhile);
gotoToken =
create_goto_token(csound,
Expand Down
10 changes: 5 additions & 5 deletions Engine/csound_orc_optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ static inline int same_type(char *var, char ty)
else return var[0]==ty;
}
*/
static inline int same_type(char *var, char ty)
{
if (var[0]=='g') return var[1]==ty;
else return var[0]==ty;
}
// static inline int same_type(char *var, char ty)
// {
// if (var[0]=='g') return var[1]==ty;
// else return var[0]==ty;
// }

#define PARSER_DEBUG1 (0)
//static TREE* remove_excess_assigns(CSOUND *csound, TREE* root)
Expand Down
2 changes: 1 addition & 1 deletion Engine/csound_type_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ CS_VARIABLE* csoundCreateVariable(CSOUND* csound, TYPE_POOL* pool,
current = current->next;
}
else ((CSOUND *)csound)->ErrorMsg(csound,
Str("cannot create variable %s: NULL type"),
Str("cannot create variable %s: NULL type\n"),
name);
return NULL;
}
Expand Down
42 changes: 22 additions & 20 deletions tests/commandline/structs/test_structs.csd
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,38 @@ struct MyType imaginary:k, real:k, kimaginary, kreal
opcode processMyType(in:MyType):(MyType)
retVal:MyType init 0, 0, 0, 0
retVal.imaginary = in.imaginary + 1
retVal.real = in.real + 1
retVal.imaginary = in.imaginary + 1
retVal.real = in.real + 1
retVal.kimaginary = in.kimaginary + 1
retVal.kreal = in.kreal + 1
retVal.kreal = in.kreal + 1
xout retVal
endop
instr 1
instr 1
var0:MyType init 1, 2, 3, 4
;var1:MyType = init:MyType(0, 0, 1, 1)
var0:MyType init 1, 2, 3, 4
;var1:MyType = init:MyType(0, 0, 1, 1)
var0.imaginary init 5
var0.real init 6
var0.kimaginary init 7
var0.kreal init 9
var0.imaginary init 5
var0.real init 6
var0.kimaginary init 7
var0.kreal init 9
var0.imaginary += 1
var0.real += 2
var0.kimaginary += 3
var0.kreal += 4
var0.imaginary += 1
var0.real += 2
var0.kimaginary += 3
var0.kreal += 4
var2:MyType processMyType var0
var2:MyType processMyType var0
; this does not work yet...
;var2:MyType = processMyType(var0)
if var0.kreal > var0.real then
prints "structs in boolean expr work!\n"
endif
; this does not work yet...
;var2:MyType = processMyType(var0)
printks "i %d r %d ki %d kr %d\n", 0.2, var0.imaginary, var0.real, var0.kimaginary, var0.kreal
printks "\ti %d r %d ki %d kr %d\n", 0.2, var2.imaginary, var2.real, var2.kimaginary, var2.kreal
printks "i %d r %d ki %d kr %d\n", 0.2, var0.imaginary, var0.real, var0.kimaginary, var0.kreal
printks "\ti %d r %d ki %d kr %d\n", 0.2, var2.imaginary, var2.real, var2.kimaginary, var2.kreal
endin
Expand All @@ -50,4 +53,3 @@ i1 0 0.5
</CsScore>
</CsoundSynthesizer>

0 comments on commit 58aa15b

Please sign in to comment.