Skip to content

Commit 6a39fcf

Browse files
committed
[[ Cleanup ]] Cleanup various parts of bytecode block patch
This patch ensures static constants are constant and use appropriate naming (k...). This patch removes warnings from emit.cpp. This patch tidies up MCScriptEmitBytecodeInModuleA This patch removes unused code including the OPCODE definition and MCLog()'s which should not have been committed.
1 parent 3064160 commit 6a39fcf

File tree

4 files changed

+152
-139
lines changed

4 files changed

+152
-139
lines changed

docs/lcb/notes/17821.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
* bytecode can now be directly written in handlers using a bytecode block:
55

6-
bytecode
7-
register tTemp
8-
assign_constant tTemp, 1
9-
end bytecode
6+
bytecode
7+
register tTemp
8+
assign_constant tTemp, 1
9+
end bytecode
1010

1111
* for more details on what bytecode operations can be used see the LiveCode
1212
Builder Bytecode Reference

libscript/src/script-builder.cpp

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct MCScriptBytecodeInfo
4545
const char *format;
4646
};
4747

48-
static const MCScriptBytecodeInfo s_bytecode_info[] =
48+
static const MCScriptBytecodeInfo kBytecodeInfo[] =
4949
{
5050
{ kMCScriptBytecodeOpJump, "jump", "l" },
5151
{ kMCScriptBytecodeOpJumpIfFalse, "jump_if_false", "rl" },
@@ -61,19 +61,19 @@ static const MCScriptBytecodeInfo s_bytecode_info[] =
6161
{ kMCScriptBytecodeOpAssignArray, "assign_array", "rr%" },
6262
{ kMCScriptBytecodeOpReset, "reset", "rr*" },
6363
};
64-
static const int s_bytecode_count = sizeof(s_bytecode_info) / sizeof(s_bytecode_info[0]);
64+
static const int kBytecodeCount = sizeof(kBytecodeInfo) / sizeof(kBytecodeInfo[0]);
6565

6666
bool MCScriptCopyBytecodeNames(MCProperListRef& r_bytecode_names)
6767
{
6868
MCAutoProperListRef t_bytecode_names;
6969
if (!MCProperListCreateMutable(&t_bytecode_names))
7070
return false;
7171

72-
for(int i = 0; i < s_bytecode_count; i++)
72+
for(int i = 0; i < kBytecodeCount; i++)
7373
{
7474
MCNewAutoNameRef t_name;
7575
if (!MCProperListPushElementOntoBack(*t_bytecode_names,
76-
MCSTR(s_bytecode_info[i].name)))
76+
MCSTR(kBytecodeInfo[i].name)))
7777
return false;
7878
}
7979

@@ -84,9 +84,9 @@ bool MCScriptCopyBytecodeNames(MCProperListRef& r_bytecode_names)
8484

8585
bool MCScriptLookupBytecode(const char *p_name, uindex_t& r_opcode)
8686
{
87-
for(uindex_t i = 0; i < s_bytecode_count; i++)
87+
for(uindex_t i = 0; i < kBytecodeCount; i++)
8888
{
89-
if (0 != strcmp(p_name, s_bytecode_info[i] . name))
89+
if (0 != strcmp(p_name, kBytecodeInfo[i] . name))
9090
continue;
9191

9292
r_opcode = i;
@@ -99,19 +99,19 @@ bool MCScriptLookupBytecode(const char *p_name, uindex_t& r_opcode)
9999

100100
const char *MCScriptDescribeBytecode(uindex_t p_opcode)
101101
{
102-
if (p_opcode >= s_bytecode_count)
102+
if (p_opcode >= kBytecodeCount)
103103
return "<unknown>";
104104

105-
return s_bytecode_info[p_opcode].name;
105+
return kBytecodeInfo[p_opcode].name;
106106
}
107107

108108
MCScriptBytecodeParameterType MCScriptDescribeBytecodeParameter(uindex_t p_opcode, uindex_t p_index)
109109
{
110-
if (p_opcode >= s_bytecode_count)
110+
if (p_opcode >= kBytecodeCount)
111111
return kMCScriptBytecodeParameterTypeUnknown;
112112

113113
const char *t_desc;
114-
t_desc = s_bytecode_info[p_opcode].format;
114+
t_desc = kBytecodeInfo[p_opcode].format;
115115

116116
size_t t_desc_length;
117117
t_desc_length = strlen(t_desc);
@@ -163,11 +163,11 @@ MCScriptBytecodeParameterType MCScriptDescribeBytecodeParameter(uindex_t p_opcod
163163

164164
bool MCScriptCheckBytecodeParameterCount(uindex_t p_opcode, uindex_t p_proposed_count)
165165
{
166-
if (p_opcode >= s_bytecode_count)
166+
if (p_opcode >= kBytecodeCount)
167167
return false;
168168

169169
const char *t_desc;
170-
t_desc = s_bytecode_info[p_opcode].format;
170+
t_desc = kBytecodeInfo[p_opcode].format;
171171

172172
size_t t_desc_length;
173173
t_desc_length = strlen(t_desc);
@@ -1335,19 +1335,6 @@ static void __end_instruction(MCScriptModuleBuilderRef self)
13351335
// Nothing to do!
13361336
}
13371337

1338-
static void __emit_instruction(MCScriptModuleBuilderRef self, MCScriptBytecodeOp p_operation, uindex_t p_arity, ...)
1339-
{
1340-
__begin_instruction(self, p_operation);
1341-
1342-
va_list t_args;
1343-
va_start(t_args, p_arity);
1344-
for(uindex_t i = 0; i < p_arity; i++)
1345-
__continue_instruction(self, va_arg(t_args, uindex_t));
1346-
va_end(t_args);
1347-
1348-
__end_instruction(self);
1349-
}
1350-
13511338
static void __emit_position(MCScriptModuleBuilderRef self, uindex_t p_address, uindex_t p_file, uindex_t p_line)
13521339
{
13531340
MCScriptPosition *t_last_pos;
@@ -1554,7 +1541,8 @@ void MCScriptResolveLabelForBytecodeInModule(MCScriptModuleBuilderRef self, uind
15541541

15551542
static MCScriptDefinitionKind __kind_of_definition(MCScriptModuleBuilderRef self, uindex_t p_index)
15561543
{
1557-
return self -> definition_kinds[p_index];
1544+
if (self -> module . definitions[p_index] == nil)
1545+
return self -> definition_kinds[p_index];
15581546

15591547
if (self -> module . definitions[p_index] -> kind == kMCScriptDefinitionKindExternal)
15601548
{
@@ -1566,12 +1554,68 @@ static MCScriptDefinitionKind __kind_of_definition(MCScriptModuleBuilderRef self
15661554
return self -> module . definitions[p_index] -> kind;
15671555
}
15681556

1557+
static bool __is_valid_definition(MCScriptModuleBuilderRef self, uindex_t p_index)
1558+
{
1559+
if (p_index >= self -> module . definition_count)
1560+
return false;
1561+
1562+
switch(__kind_of_definition(self, p_index))
1563+
{
1564+
case kMCScriptDefinitionKindVariable:
1565+
case kMCScriptDefinitionKindHandler:
1566+
case kMCScriptDefinitionKindForeignHandler:
1567+
case kMCScriptDefinitionKindConstant:
1568+
return true;
1569+
1570+
default:
1571+
break;
1572+
}
1573+
1574+
return false;
1575+
}
1576+
1577+
static bool __is_valid_variable_definition(MCScriptModuleBuilderRef self, uindex_t p_index)
1578+
{
1579+
if (p_index >= self -> module . definition_count)
1580+
return false;
1581+
1582+
switch(__kind_of_definition(self, p_index))
1583+
{
1584+
case kMCScriptDefinitionKindVariable:
1585+
return true;
1586+
1587+
default:
1588+
break;
1589+
}
1590+
1591+
return false;
1592+
}
1593+
1594+
static bool __is_valid_handler_definition(MCScriptModuleBuilderRef self, uindex_t p_index)
1595+
{
1596+
if (p_index >= self -> module . definition_count)
1597+
return false;
1598+
1599+
switch(__kind_of_definition(self, p_index))
1600+
{
1601+
case kMCScriptDefinitionKindHandler:
1602+
case kMCScriptDefinitionKindForeignHandler:
1603+
case kMCScriptDefinitionKindDefinitionGroup:
1604+
return true;
1605+
1606+
default:
1607+
break;
1608+
}
1609+
1610+
return false;
1611+
}
1612+
15691613
void MCScriptEmitBytecodeInModuleA(MCScriptModuleBuilderRef self, uindex_t p_opcode, uindex_t *p_arguments, uindex_t p_argument_count)
15701614
{
15711615
if (self == nil || !self -> valid)
15721616
return;
15731617

1574-
if (p_opcode >= s_bytecode_count ||
1618+
if (p_opcode >= kBytecodeCount ||
15751619
!MCScriptCheckBytecodeParameterCount(p_opcode, p_argument_count))
15761620
{
15771621
self -> valid = false;
@@ -1611,31 +1655,23 @@ void MCScriptEmitBytecodeInModuleA(MCScriptModuleBuilderRef self, uindex_t p_opc
16111655
break;
16121656

16131657
case kMCScriptBytecodeParameterTypeDefinition:
1614-
if (p_arguments[i] >= self -> module . definition_count ||
1615-
!(__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindVariable ||
1616-
__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindHandler ||
1617-
__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindForeignHandler ||
1618-
__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindConstant))
1658+
if (!__is_valid_definition(self, p_arguments[i]))
16191659
{
16201660
self -> valid = false;
16211661
return;
16221662
}
16231663
break;
16241664

16251665
case kMCScriptBytecodeParameterTypeVariable:
1626-
if (p_arguments[i] >= self -> module . definition_count ||
1627-
__kind_of_definition(self, p_arguments[i]) != kMCScriptDefinitionKindVariable)
1666+
if (!__is_valid_variable_definition(self, p_arguments[i]))
16281667
{
16291668
self -> valid = false;
16301669
return;
16311670
}
16321671
break;
16331672

16341673
case kMCScriptBytecodeParameterTypeHandler:
1635-
if (p_arguments[i] >= self -> module . definition_count ||
1636-
!(__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindHandler ||
1637-
__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindForeignHandler ||
1638-
__kind_of_definition(self, p_arguments[i]) == kMCScriptDefinitionKindDefinitionGroup))
1674+
if (!__is_valid_handler_definition(self, p_arguments[i]))
16391675
{
16401676
self -> valid = false;
16411677
return;
@@ -1645,7 +1681,7 @@ void MCScriptEmitBytecodeInModuleA(MCScriptModuleBuilderRef self, uindex_t p_opc
16451681
}
16461682

16471683
__begin_instruction(self,
1648-
s_bytecode_info[p_opcode] . opcode);
1684+
kBytecodeInfo[p_opcode] . opcode);
16491685
for(uindex_t i = 0; i < p_argument_count; i++)
16501686
__continue_instruction(self,
16511687
p_arguments[i]);

0 commit comments

Comments
 (0)