Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External mode compiler: Compile-time evaluate most constant subexpressions #5482

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ Major changes from 1.9.0-jumbo-1 (May 2019) in this bleeding-edge version:

- Add Combinator external mode (combines words into pairs). [Solar; 2024]

- External mode compiler: Compile-time evaluate most constant subexpressions.
[Solar; 2024]


Major changes from 1.8.0-jumbo-1 (December 2014) to 1.9.0-jumbo-1 (May 2019):

Expand Down
118 changes: 80 additions & 38 deletions src/compiler.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2000,2003,2005,2011-2013,2015,2018 by Solar Designer
* Copyright (c) 1996-2000,2003,2005,2011-2013,2015,2018,2024 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
Expand Down Expand Up @@ -104,12 +104,15 @@ static char *c_reserved[] = {
#define C_CLASS_LEFT 1
#define C_CLASS_RIGHT 2

#define C_LVALUE 1

struct c_op {
int prec;
int dir;
int class;
char *name;
void (*op)(void);
int lvalue;
};

#ifdef __GNUC__
Expand Down Expand Up @@ -437,6 +440,60 @@ static void (*c_push
return last;
}

static void c_subexpr(void (**last)(void), const struct c_op *op)
{
/* Compile-time evaluate binary op after push_imm_imm */
if (op->class == C_CLASS_BINARY && *last == c_op_push_imm_imm) {
if (op->lvalue) {
c_errno = C_ERROR_EXPR;
return;
}
/* Patch push_imm_imm to push_imm of operation result */
*last = c_op_push_imm;
if (c_pass) {
c_stack[1].mem = &(c_code_ptr - 2)->imm;
(c_code_ptr++)->op = op->op;
(c_code_ptr++)->op = c_op_assign_pop;
(c_code_ptr++)->op = c_op_return;
c_execute_fast(c_code_ptr -= 6);
c_code_ptr->op = c_op_push_imm;
c_code_ptr += 2;
} else
c_code_ptr--;
return;
}

/* Compile-time evaluate binary op after push_*_imm, push_imm */
/* Unimplemented, need to start tracking penultimate op first */

/* Compile-time evaluate unary op after any push ending in imm */
if (op->class != C_CLASS_BINARY && (*last == c_op_push_imm ||
*last == c_op_push_imm_imm || *last == c_op_push_mem_imm ||
*last == c_op_push_mem_mem_mem_imm)) {
if (op->lvalue) {
c_errno = C_ERROR_EXPR;
return;
}
/* Patch the last immediate operand with operation result */
if (c_pass) {
c_stack[1].mem = &(c_code_ptr - 1)->imm;
c_int imm = (c_code_ptr - 1)->imm;
(c_code_ptr++)->op = c_op_push_imm;
(c_code_ptr++)->imm = imm;
(c_code_ptr++)->op = op->op;
(c_code_ptr++)->op = c_op_assign_pop;
(c_code_ptr++)->op = c_op_return;
c_execute_fast(c_code_ptr -= 5);
}
return;
}

*last = op->op;
if (c_pass)
c_code_ptr->op = op->op;
c_code_ptr++;
}

static int c_block(char term, struct c_ident *vars);

static int c_define(char term, struct c_ident **vars, struct c_ident *globals)
Expand Down Expand Up @@ -521,33 +578,21 @@ static int c_expr(char term, struct c_ident *vars, char *token, int pop)
if (c_ops[stack[sp]].class == C_CLASS_BINARY)
balance--;

last = c_ops[stack[sp]].op;
if (c_pass)
c_code_ptr->op = last;
c_code_ptr++;
c_subexpr(&last, &c_ops[stack[sp]]);

if (!stack[sp]) break;
}

if ((c == ')' && stack[sp] >= 0) ||
(c == ']' && stack[sp]) ||
((c == ';' || (term != ')' && c == term)) && sp))
c_errno = C_ERROR_COUNT;
c_errno = C_ERROR_EXPR;
if (c_errno || (!sp && c == term)) break;

left = 1;
} else
if ((c >= '0' && c <= '9') || c == '\'' || (c == '-' && !left)) {
if (c == '-') {
lookahead = c_getchar(0);
c_ungetchar(lookahead);
if (lookahead < '0' || lookahead > '9')
goto other;
token = c_gettoken();
}
if ((c >= '0' && c <= '9') || c == '\'') {
value.imm = c_getint(token);
if (c == '-')
value.imm = -value.imm;
last = c_push(last, c_op_push_imm, &value);

left = 1; balance++;
Expand All @@ -561,7 +606,6 @@ static int c_expr(char term, struct c_ident *vars, char *token, int pop)
left = 0;
} else
if (c != ' ') {
other:
if (c_isident[ARCH_INDEX(c)])
var = c_find_ident(vars, NULL, token);
else
Expand Down Expand Up @@ -607,10 +651,7 @@ static int c_expr(char term, struct c_ident *vars, char *token, int pop)
if (op2->class == C_CLASS_BINARY)
balance--;

last = op2->op;
if (c_pass)
c_code_ptr->op = last;
c_code_ptr++;
c_subexpr(&last, op2);

sp--;
}
Expand All @@ -631,7 +672,7 @@ static int c_expr(char term, struct c_ident *vars, char *token, int pop)

if (c_errno) return c_errno;

if (sp || balance) c_errno = C_ERROR_COUNT;
if (sp || balance) c_errno = C_ERROR_EXPR;

if (pop) {
if (last == c_op_assign) {
Expand Down Expand Up @@ -872,7 +913,8 @@ int c_compile(int (*ext_getchar)(void), void (*ext_rewind)(void),

if (c_errno || c_pass) break;

c_code_start = mem_alloc((size_t)c_code_ptr);
/* 5 extra slots for temporary code during constant subexpression evaluation */
c_code_start = mem_alloc((size_t)(c_code_ptr + 5));
c_data_start = mem_alloc((size_t)c_data_ptr);
memset(c_data_start, 0, (size_t)c_data_ptr);
}
Expand Down Expand Up @@ -1622,17 +1664,17 @@ static void (*c_op_assign_pop)(void) = c_f_op_assign_pop;
/* Must be in the same order as ops[] in c_execute_fast() */
static struct c_op c_ops[] = {
{1, C_LEFT_TO_RIGHT, C_CLASS_BINARY, "[", c_op_index},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "=", c_f_op_assign},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "+=", c_op_add_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "-=", c_op_sub_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "*=", c_op_mul_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "/=", c_op_div_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "%=", c_op_mod_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "|=", c_op_or_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "^=", c_op_xor_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "&=", c_op_and_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "<<=", c_op_shl_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, ">>=", c_op_shr_a},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "=", c_f_op_assign, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "+=", c_op_add_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "-=", c_op_sub_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "*=", c_op_mul_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "/=", c_op_div_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "%=", c_op_mod_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "|=", c_op_or_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "^=", c_op_xor_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "&=", c_op_and_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, "<<=", c_op_shl_a, C_LVALUE},
{2, C_RIGHT_TO_LEFT, C_CLASS_BINARY, ">>=", c_op_shr_a, C_LVALUE},
{3, C_LEFT_TO_RIGHT, C_CLASS_BINARY, "||", c_op_or_i},
{4, C_LEFT_TO_RIGHT, C_CLASS_BINARY, "&&", c_op_and_b},
{5, C_LEFT_TO_RIGHT, C_CLASS_BINARY, "|", c_op_or_i},
Expand All @@ -1654,10 +1696,10 @@ static struct c_op c_ops[] = {
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "!", c_op_not_b},
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "~", c_op_not_i},
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "-", c_op_neg},
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "++", c_op_inc_l},
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "--", c_op_dec_l},
{14, C_LEFT_TO_RIGHT, C_CLASS_RIGHT, "++", c_op_inc_r},
{14, C_LEFT_TO_RIGHT, C_CLASS_RIGHT, "--", c_op_dec_r},
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "++", c_op_inc_l, C_LVALUE},
{13, C_RIGHT_TO_LEFT, C_CLASS_LEFT, "--", c_op_dec_l, C_LVALUE},
{14, C_LEFT_TO_RIGHT, C_CLASS_RIGHT, "++", c_op_inc_r, C_LVALUE},
{14, C_LEFT_TO_RIGHT, C_CLASS_RIGHT, "--", c_op_dec_r, C_LVALUE},
#ifdef PRINT_INSNS
{0, 0, 0, "return", c_f_op_return},
{0, 0, 0, "bz", c_f_op_bz},
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#define C_ERROR_NONE 0
#define C_ERROR_UNKNOWN 1
#define C_ERROR_UNEXPECTED 2
#define C_ERROR_COUNT 3
#define C_ERROR_EXPR 3
#define C_ERROR_TOOLONG 4
#define C_ERROR_TOOCOMPLEX 5
#define C_ERROR_ARRAYSIZE 6
Expand Down