Skip to content

Commit

Permalink
o codes stack optimization
Browse files Browse the repository at this point in the history
- optimized o-codes stack by moving local volatile vars to a context stack
- use param modifier stack on parser state to perform variable modification
- global numbered parameters removed from parser state
  • Loading branch information
Paciente8159 committed Oct 25, 2024
1 parent 4518027 commit 953b930
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 59 deletions.
4 changes: 4 additions & 0 deletions makefiles/virtual/o100.nc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ G0X10
O100 RETURN
G0X10

O101 ELSEIF [#1 EQ 11]
#3=100 (local volatible)
#31=100 (global volatile)

O101 ELSE
(msg, do O call)
O110 CALL [#1]
Expand Down
32 changes: 24 additions & 8 deletions uCNC/src/core/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static void parser_coordinate_system_load(uint8_t param, float *target);

#ifdef ENABLE_RS274NGC_EXPRESSIONS
extern char parser_backtrack;
extern bool o_code_end_subrotine(parser_state_t *new_state);
extern bool o_code_end_subrotine(void);
extern bool o_code_returned;
extern float o_code_return_value;
extern uint8_t o_code_stack_index;
Expand Down Expand Up @@ -847,21 +847,27 @@ static uint8_t parser_fetch_command(parser_state_t *new_state, parser_words_t *w
return STATUS_BAD_NUMBER_FORMAT;
}
DBGMSG("Assign #%lu=%f", (uint32_t)value, assign_val);
new_state->user_vars[(int)value - 1] = assign_val;
if (new_state->modified_params_count >= RS274NGC_MAX_PARAMS_SET_PER_LINE)
{
return STATUS_MAXIMUM_PARAMS_PER_BLOCK_EXCEEDED;
}
new_state->modified_params[new_state->modified_params_count].id = (uint16_t)value;
new_state->modified_params[new_state->modified_params_count].value = assign_val;
new_state->modified_params_count++;
break;
#ifdef ENABLE_O_CODES
case 'O':
error = parser_ocode_word((uint16_t)truncf(value), new_state, cmd);
if (error != STATUS_OK)
{
// an error on a subrotine will cause the top subrotine to close and the stack to collapse
while (o_code_end_subrotine(new_state))
while (o_code_end_subrotine())
;
return error;
}
break;
case FILE_EOF:
if (o_code_end_subrotine(new_state))
if (o_code_end_subrotine())
{
break;
}
Expand Down Expand Up @@ -2007,6 +2013,7 @@ static uint8_t parser_gcode_command(bool is_jogging)
next_state.groups.nonmodal = 0; // reset nonmodal

#ifdef ENABLE_RS274NGC_EXPRESSIONS
// reset modified params
next_state.modified_params_count = 0;
memset(next_state.modified_params, 0, sizeof(next_state.modified_params));
#endif
Expand Down Expand Up @@ -2045,6 +2052,13 @@ static uint8_t parser_gcode_command(bool is_jogging)
// if is jog motion state is not preserved
if (!is_jogging)
{
#ifdef ENABLE_RS274NGC_EXPRESSIONS
// stores the new parameters
for (uint8_t i = 0; i < next_state.modified_params_count; i++)
{
parser_set_parameter(next_state.modified_params[i].id, next_state.modified_params[i].value);
}
#endif
// if everything went ok updates the parser modal groups and position
memcpy(&parser_state, &next_state, sizeof(parser_state_t));
}
Expand Down Expand Up @@ -3207,8 +3221,10 @@ uint8_t parser_exec_command_block(parser_state_t *new_state, parser_words_t *wor
*/
#ifdef ENABLE_RS274NGC_EXPRESSIONS

float g_parser_num_params[RS274NGC_MAX_USER_VARS];

#ifdef ENABLE_NAMED_PARAMETERS
float parser_get_named_parameter(int param, int offset, uint8_t pos)
static float parser_get_named_parameter(int param, int offset, uint8_t pos)
{
float result = -1;
switch (offset)
Expand Down Expand Up @@ -3367,7 +3383,7 @@ float parser_get_parameter(uint16_t param)

if (param > 0 && param <= RS274NGC_MAX_USER_VARS)
{
return parser_state.user_vars[param - 1];
return g_parser_num_params[param - 1];
}

switch (offset)
Expand Down Expand Up @@ -3463,11 +3479,11 @@ float parser_get_parameter(uint16_t param)
return 0;
}

float parser_set_parameter(uint16_t param, float value)
void parser_set_parameter(uint16_t param, float value)
{
if (param > 0 && param <= RS274NGC_MAX_USER_VARS)
{
return parser_state.user_vars[param - 1] = value;
g_parser_num_params[param - 1] = value;
}
}

Expand Down
5 changes: 2 additions & 3 deletions uCNC/src/core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ extern "C"
uint32_t line;
#endif
#ifdef ENABLE_RS274NGC_EXPRESSIONS
float user_vars[MIN(RS274NGC_MAX_USER_VARS, 30)];
uint8_t modified_params_count;
parser_param_modif_t modified_params[RS274NGC_MAX_PARAMS_SET_PER_LINE];
#endif
Expand All @@ -331,10 +330,10 @@ extern "C"
unsigned char parser_get_next_preprocessed(bool peek);
#ifdef ENABLE_RS274NGC_EXPRESSIONS
float parser_get_parameter(uint16_t param);
float parser_set_parameter(uint16_t param, float value);
void parser_set_parameter(uint16_t param, float value);
#ifdef ENABLE_O_CODES
uint8_t parser_ocode_word(uint16_t code, parser_state_t *new_state, parser_cmd_explicit_t *cmd);
bool o_code_end_subrotine(parser_state_t *new_state);
bool o_code_end_subrotine(void);
#endif
#ifdef ENABLE_NAMED_PARAMETERS
uint8_t parser_get_namedparam_id(float *value);
Expand Down
69 changes: 40 additions & 29 deletions uCNC/src/core/parser_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
*/
#ifdef ENABLE_RS274NGC_EXPRESSIONS
char parser_backtrack;
extern float g_parser_num_params[RS274NGC_MAX_USER_VARS];
#define STRLEN(s) (sizeof(s) / sizeof(s[0]))
#define STRCMP(sram, srom) rom_strcmp(sram, __romstr__(srom))
#ifdef ENABLE_NAMED_PARAMETERS
Expand Down Expand Up @@ -160,14 +161,16 @@ static const named_param_t named_params[] __rom__ = {
NAMED_PARAM(_task, 6110),
NAMED_PARAM(_call_level, 6111),
NAMED_PARAM(_remap_level, 6112)};

static float parser_get_named_parameter(int param, int offset, uint8_t pos);
#endif
#ifdef ENABLE_O_CODES
#include "../modules/file_system.h"
#ifndef OCODE_STACK_DEPTH
#define OCODE_STACK_DEPTH MAX_PARSER_STACK_DEPTH
#ifndef OCODE_PARSER_STACK_DEPTH
#define OCODE_PARSER_STACK_DEPTH MAX_PARSER_STACK_DEPTH
#endif
#ifndef OCODE_CONTEXT_STACK_DEPTH
#define OCODE_CONTEXT_STACK_DEPTH 10
#endif
#define OCODE_CONTEXT_SIZE (sizeof(float) * MIN(RS274NGC_MAX_USER_VARS, 30))
#ifndef OCODE_DRIVE
#define OCODE_DRIVE 'C'
#endif
Expand Down Expand Up @@ -210,11 +213,13 @@ typedef struct o_code_stack_
uint8_t op;
uint32_t pos;
int32_t loop;
float user_vars[MIN(RS274NGC_MAX_USER_VARS, 30)];
uint8_t context_index;
} o_code_stack_t;

static o_code_stack_t o_code_stack[OCODE_STACK_DEPTH];
static o_code_stack_t o_code_stack[OCODE_PARSER_STACK_DEPTH];
uint8_t o_code_stack_index;
uint8_t o_code_stack_context_index;
float o_code_stack_context_vars[OCODE_CONTEXT_STACK_DEPTH][MIN(RS274NGC_MAX_USER_VARS, 30)];
#define O_CODE_FILE_CLOSE 1
#define O_CODE_FILE_CLOSE_ALL 2
#endif
Expand Down Expand Up @@ -837,6 +842,19 @@ static void o_code_open(uint8_t index)
}
}

static uint8_t o_code_entry_point(uint8_t index)
{
// find previous sub entry point
while (o_code_stack[index].op != O_CODE_OP_SUB && index)
{
o_code_stack[index].code = 0;
o_code_stack[index].op = 0;
index--;
}

return index;
}

static uint8_t o_code_close(uint8_t index)
{
// close file
Expand All @@ -848,41 +866,31 @@ static uint8_t o_code_close(uint8_t index)
o_code_file = NULL;
}

// find previous sub entry point
while (o_code_stack[index].op != O_CODE_OP_SUB && index)
{
o_code_stack[index].code = 0;
o_code_stack[index].op = 0;
index--;
}

// restore file pointer and mark entry for (re)call
o_code_file_pos = o_code_stack[index].pos;
o_code_stack[index].op = O_CODE_OP_CALL;
o_code_stack_index = index;
index = o_code_entry_point(index);
}

return index;
}

bool o_code_end_subrotine(parser_state_t *new_state)
bool o_code_end_subrotine(void)
{
uint8_t index = o_code_stack_index;
if (index)
{
index = o_code_close(index);
// restore user vars
memcpy(new_state->user_vars, o_code_stack[index].user_vars, sizeof(o_code_stack[index].user_vars));
memcpy(g_parser_num_params, o_code_stack_context_vars[o_code_stack[index].context_index], OCODE_CONTEXT_SIZE);
o_code_stack_context_index = o_code_stack[index].context_index;

// restore file pointer and mark entry for (re)call
o_code_file_pos = o_code_stack[index].pos;
memset(&o_code_stack[index], 0, sizeof(o_code_stack_t));
}

if (index)
{
index--;
// find previous sub entry point
while (o_code_stack[index].op != O_CODE_OP_SUB && index)
{
index--;
}
index = o_code_entry_point(index);
// top sub not exited yet
o_code_open(index);

Expand All @@ -897,6 +905,7 @@ bool o_code_end_subrotine(parser_state_t *new_state)
}
memset(o_code_stack, 0, sizeof(o_code_stack));
o_code_stack_index = 0;
o_code_stack_context_index = 0;
// grbl_stream_change(NULL);
grbl_stream_readonly(o_code_file_flush, NULL, NULL);
return false;
Expand Down Expand Up @@ -964,7 +973,7 @@ static void FORCEINLINE o_code_word_error(uint8_t *error)
{
for (uint16_t i = 0; i < RS274NGC_MAX_USER_VARS; i++)
{
parser_set_parameter(i + 1, o_code_stack[0].user_vars[i]);
parser_set_parameter(i + 1, o_code_stack_context_vars[0][i]);
}
}
memset(o_code_stack, 0, sizeof(o_code_stack));
Expand Down Expand Up @@ -1021,7 +1030,8 @@ uint8_t parser_ocode_word(uint16_t code, parser_state_t *new_state, parser_cmd_e
if (!STRCMP(o_cmd, "CALL"))
{
// store user vars
memcpy(o_code_stack[index].user_vars, new_state->user_vars, sizeof(o_code_stack[index].user_vars));
uint8_t i_context = o_code_stack_context_index++;
memcpy(&o_code_stack_context_vars[i_context], g_parser_num_params, OCODE_CONTEXT_SIZE);

// check if file exists
char o_subrotine[32];
Expand All @@ -1040,7 +1050,7 @@ uint8_t parser_ocode_word(uint16_t code, parser_state_t *new_state, parser_cmd_e
while (op_arg_error == NUMBER_OK)
{
// load args
new_state->user_vars[arg_i++] = op_arg;
g_parser_num_params[arg_i++] = op_arg;
op_arg_error = parser_get_float(&op_arg);
}

Expand All @@ -1049,6 +1059,7 @@ uint8_t parser_ocode_word(uint16_t code, parser_state_t *new_state, parser_cmd_e
// store operation in the stack
o_code_stack[index].code = ocode_id;
o_code_stack[index].op = O_CODE_OP_CALL;
o_code_stack[index].context_index = i_context;

// workaround to ftell
o_code_stack[index].pos = (o_code_file) ? (o_code_file->file_info.size - fs_available(o_code_file)) : 0;
Expand Down Expand Up @@ -1141,7 +1152,7 @@ uint8_t parser_ocode_word(uint16_t code, parser_state_t *new_state, parser_cmd_e
{
bool found = (o_code_stack[index].code == ocode_id);
o_code_stack_index = index;
o_code_end_subrotine(new_state);
o_code_end_subrotine();
if (found)
{
error = STATUS_OK;
Expand Down
1 change: 1 addition & 0 deletions uCNC/src/interface/grbl_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ extern "C"
#define STATUS_HARDLIMITS_DISABLED 57
#define STATUS_STREAM_FAILED 58
#define STATUS_JOG_CANCELED 59
#define STATUS_MAXIMUM_PARAMS_PER_BLOCK_EXCEEDED 60
#define STATUS_GCODE_EXTENDED_UNSUPPORTED 254 // deprecated
#define STATUS_CRITICAL_FAIL 255

Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/modules/file_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ extern "C"
bool fs_next_file(fs_file_t *fp, fs_file_info_t *finfo);
bool fs_finfo(const char *path, fs_file_info_t *finfo);

static void fs_safe_free(void* fp){
static __attribute__((unused)) void fs_safe_free(void* fp){
if(fp){
free(fp);
fp = NULL;
Expand Down
18 changes: 0 additions & 18 deletions uCNC/uCNC.ino.cpp

This file was deleted.

0 comments on commit 953b930

Please sign in to comment.