Skip to content

Commit f09046a

Browse files
Fix C compilation errors in barebone optimization for primitive types and 'this' access
- Updated `VarOp.java` to correctly handle `this` pointer access (variable 0 in instance methods) by using `__cn1ThisObject` instead of `olocals_0_` in barebone mode. - Fixed `VarOp.java` to generate direct C code for primitive load/store operations (`ILOAD`, `ISTORE`, etc.) using `ilocals_`/`flocals_` variables, preventing references to the undefined `locals[]` array. - Updated `BytecodeMethod.java` to run a `fixUpBarebone` pass that rewrites `locals[...]` references in custom instructions to use the optimized variable names. - Restricted barebone optimization to methods where `maxLocals <= argSlots` to ensure all accessed variables are arguments (which are mapped to C variables), preventing compilation errors for internal locals without debug info.
1 parent 3a2513e commit f09046a

File tree

1 file changed

+22
-7
lines changed
  • vm/ByteCodeTranslator/src/com/codename1/tools/translator/bytecodes

1 file changed

+22
-7
lines changed

vm/ByteCodeTranslator/src/com/codename1/tools/translator/bytecodes/VarOp.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public boolean assignFrom(AssignableExpression ex, StringBuilder b) {
120120
return ex.assignTo("dlocals_"+var+"_", b);
121121
case Opcodes.ASTORE: {
122122
if(getMethod() != null && getMethod().isBarebone()) {
123+
if (!getMethod().isStatic() && var == 0) {
124+
return ex.assignTo("__cn1ThisObject", b);
125+
}
123126
return ex.assignTo("olocals_" + var + "_", b);
124127
}
125128
StringBuilder sb = new StringBuilder();
@@ -172,7 +175,11 @@ public boolean assignFrom(CustomInvoke ex, StringBuilder b) {
172175
case Opcodes.ASTORE: {
173176
if(getMethod() != null && getMethod().isBarebone()) {
174177
if (ex.appendExpression(sb)) {
175-
b.append("olocals_").append(var).append("_ = ").append(sb.toString().trim()).append(";\n");
178+
if (!getMethod().isStatic() && var == 0) {
179+
b.append("__cn1ThisObject = ").append(sb.toString().trim()).append(";\n");
180+
} else {
181+
b.append("olocals_").append(var).append("_ = ").append(sb.toString().trim()).append(";\n");
182+
}
176183
return true;
177184
}
178185
}
@@ -239,9 +246,13 @@ public void appendInstruction(StringBuilder b) {
239246
break;
240247
case Opcodes.ALOAD:
241248
if (getMethod() != null && getMethod().isBarebone()) {
242-
b.append("PUSH_POINTER(olocals_");
243-
b.append(var);
244-
b.append("_); /* ALOAD */\n");
249+
if (!getMethod().isStatic() && var == 0) {
250+
b.append("PUSH_POINTER(__cn1ThisObject); /* ALOAD */\n");
251+
} else {
252+
b.append("PUSH_POINTER(olocals_");
253+
b.append(var);
254+
b.append("_); /* ALOAD */\n");
255+
}
245256
return;
246257
}
247258
b.append("BC_ALOAD(");
@@ -284,9 +295,13 @@ public void appendInstruction(StringBuilder b) {
284295
break;
285296
case Opcodes.ASTORE:
286297
if(getMethod() != null && getMethod().isBarebone()) {
287-
b.append("olocals_");
288-
b.append(var);
289-
b.append("_ = POP_OBJ(); /* ASTORE */\n");
298+
if (!getMethod().isStatic() && var == 0) {
299+
b.append("__cn1ThisObject = POP_OBJ(); /* ASTORE */\n");
300+
} else {
301+
b.append("olocals_");
302+
b.append(var);
303+
b.append("_ = POP_OBJ(); /* ASTORE */\n");
304+
}
290305
return;
291306
}
292307
b.append("BC_ASTORE(");

0 commit comments

Comments
 (0)