Skip to content
Open
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
9 changes: 8 additions & 1 deletion vm/ByteCodeTranslator/src/nativeMethods.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "java_lang_Float.h"
#include "java_lang_Runnable.h"
#include "java_lang_Throwable.h"
#include "java_lang_StackOverflowError.h"
#include "java_lang_VirtualMachineError.h"
#include "java_lang_StringBuilder.h"
#include "java_util_HashMap.h"
#include "java_util_HashMap_Entry.h"
Expand Down Expand Up @@ -1550,9 +1552,14 @@ void initMethodStack(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT __cn1ThisObject, int
THROW_NULL_POINTER_EXCEPTION();
}
#endif
if (threadStateData->callStackOffset >= CN1_MAX_STACK_CALL_DEPTH - 1) {
JAVA_OBJECT stackOverflow = __NEW_INSTANCE_java_lang_StackOverflowError(threadStateData);
java_lang_Throwable_fillInStack__(threadStateData, stackOverflow);
throwException(threadStateData, stackOverflow);
return;
}
memset(&threadStateData->threadObjectStack[threadStateData->threadObjectStackOffset], 0, sizeof(struct elementStruct) * (localsStackSize + stackSize));
threadStateData->threadObjectStackOffset += localsStackSize + stackSize;
CODENAME_ONE_ASSERT(threadStateData->callStackOffset < CN1_MAX_STACK_CALL_DEPTH - 1);
threadStateData->callStackClass[threadStateData->callStackOffset] = classNameId;
threadStateData->callStackMethod[threadStateData->callStackOffset] = methodNameId;
threadStateData->callStackOffset++;
Expand Down
44 changes: 44 additions & 0 deletions vm/JavaAPI/src/java/lang/StackOverflowError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/

package java.lang;

/**
* Thrown when a stack overflow occurs because an application recurses too deeply.
* Since: JDK1.0, CLDC 1.0
*/
public class StackOverflowError extends VirtualMachineError {
/**
* Constructs a StackOverflowError with no detail message.
*/
public StackOverflowError() {
}

/**
* Constructs a StackOverflowError with the specified detail message.
* @param s the detail message.
*/
public StackOverflowError(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ void translatesOptimizedBytecodeToLLVMExecutable(CompilerHelper.CompilerConfig c
Path buildDir = distDir.resolve("build");
Files.createDirectories(buildDir);

CleanTargetIntegrationTest.runCommand(Arrays.asList(
List<String> cmakeCommand = new ArrayList<>(Arrays.asList(
"cmake",
"-S", distDir.toString(),
"-B", buildDir.toString(),
"-DCMAKE_C_COMPILER=clang",
"-DCMAKE_OBJC_COMPILER=clang"
), distDir);
"-B", buildDir.toString()
));
cmakeCommand.addAll(CleanTargetIntegrationTest.cmakeCompilerArgs());
CleanTargetIntegrationTest.runCommand(cmakeCommand, distDir);

CleanTargetIntegrationTest.runCommand(Arrays.asList("cmake", "--build", buildDir.toString()), distDir);

Expand Down Expand Up @@ -293,13 +293,13 @@ void translatesInvokeAndLdcBytecodeToLLVMExecutable(CompilerHelper.CompilerConfi
Path buildDir = distDir.resolve("build");
Files.createDirectories(buildDir);

CleanTargetIntegrationTest.runCommand(Arrays.asList(
List<String> cmakeCommand = new ArrayList<>(Arrays.asList(
"cmake",
"-S", distDir.toString(),
"-B", buildDir.toString(),
"-DCMAKE_C_COMPILER=clang",
"-DCMAKE_OBJC_COMPILER=clang"
), distDir);
"-B", buildDir.toString()
));
cmakeCommand.addAll(CleanTargetIntegrationTest.cmakeCompilerArgs());
CleanTargetIntegrationTest.runCommand(cmakeCommand, distDir);

CleanTargetIntegrationTest.runCommand(Arrays.asList("cmake", "--build", buildDir.toString()), distDir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -100,17 +101,18 @@ void generatesRunnableHelloWorldUsingCleanTarget(CompilerHelper.CompilerConfig c
writeRuntimeStubs(srcRoot);

replaceLibraryWithExecutableTarget(cmakeLists, srcRoot.getFileName().toString());
relaxLiteralRangeWarnings(cmakeLists);

Path buildDir = distDir.resolve("build");
Files.createDirectories(buildDir);

runCommand(Arrays.asList(
List<String> cmakeCommand = new java.util.ArrayList<>(Arrays.asList(
"cmake",
"-S", distDir.toString(),
"-B", buildDir.toString(),
"-DCMAKE_C_COMPILER=clang",
"-DCMAKE_OBJC_COMPILER=clang"
), distDir);
"-B", buildDir.toString()
));
cmakeCommand.addAll(cmakeCompilerArgs());
runCommand(cmakeCommand, distDir);

runCommand(Arrays.asList("cmake", "--build", buildDir.toString()), distDir);

Expand Down Expand Up @@ -188,6 +190,71 @@ static void replaceLibraryWithExecutableTarget(Path cmakeLists, String sourceDir
Files.write(cmakeLists, replacement.getBytes(StandardCharsets.UTF_8));
}

static void relaxLiteralRangeWarnings(Path cmakeLists) throws IOException {
final String marker = "CN1_LITERAL_RANGE_FLAGS";
String content = new String(Files.readAllBytes(cmakeLists), StandardCharsets.UTF_8);
if (content.contains(marker)) {
return;
}

String newline = "\n";
StringBuilder block = new StringBuilder();
block.append(newline)
.append("# CN1 literal-range warning relaxation").append(newline)
.append("set(").append(marker).append(" \"-Wno-error=literal-range -Wno-literal-range\")").append(newline)
.append("if (CMAKE_C_COMPILER_ID MATCHES \"Clang\")").append(newline)
.append(" set(CMAKE_C_FLAGS \"${CMAKE_C_FLAGS} ${").append(marker).append("}\")").append(newline)
.append("endif").append(newline)
.append("if (CMAKE_OBJC_COMPILER_ID MATCHES \"Clang\")").append(newline)
.append(" set(CMAKE_OBJC_FLAGS \"${CMAKE_OBJC_FLAGS} ${").append(marker).append("}\")").append(newline)
.append("endif").append(newline);

StringBuilder amended = new StringBuilder(content);
if (!content.endsWith("\n") && !content.endsWith("\r\n")) {
amended.append(newline);
}
amended.append(block);

Files.write(cmakeLists, amended.toString().getBytes(StandardCharsets.UTF_8));
}

static List<String> cmakeCompilerArgs() {
List<String> args = new java.util.ArrayList<>();
String cCompiler = findExecutable(Arrays.asList("clang", "cc", "gcc"));
if (cCompiler != null) {
args.add("-DCMAKE_C_COMPILER=" + cCompiler);
}
String objcCompiler = findExecutable(Arrays.asList("clang", "gcc", "cc"));
if (objcCompiler != null) {
args.add("-DCMAKE_OBJC_COMPILER=" + objcCompiler);
}
return args;
}

private static String findExecutable(List<String> candidates) {
for (String candidate : candidates) {
Path found = findOnPath(candidate);
if (found != null) {
return found.toString();
}
}
return null;
}

private static Path findOnPath(String executable) {
String path = System.getenv("PATH");
if (path == null) {
return null;
}
for (String dir : path.split(File.pathSeparator)) {
Path candidate = Paths.get(dir, executable);
if (Files.isExecutable(candidate)) {
return candidate;
}
}
return null;
}

static String runCommand(List<String> command, Path workingDir) throws Exception {
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(workingDir.toFile());
Expand All @@ -202,6 +269,29 @@ static String runCommand(List<String> command, Path workingDir) throws Exception
return output;
}

static CommandResult runCommandWithResult(List<String> command, Path workingDir) throws Exception {
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(workingDir.toFile());
builder.redirectErrorStream(true);
Process process = builder.start();
String output;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
output = reader.lines().collect(Collectors.joining("\n"));
}
int exit = process.waitFor();
return new CommandResult(exit, output);
}

static final class CommandResult {
final int exitCode;
final String output;

CommandResult(int exitCode, String output) {
this.exitCode = exitCode;
this.output = output;
}
}

static void patchCn1Globals(Path srcRoot) throws IOException {
Path cn1Globals = srcRoot.resolve("cn1_globals.h");
String content = new String(Files.readAllBytes(cn1Globals), StandardCharsets.UTF_8);
Expand All @@ -215,6 +305,22 @@ static void patchCn1Globals(Path srcRoot) throws IOException {
}
}

static void patchStaticGetterPrototypes(Path srcRoot) throws IOException {
Files.walk(srcRoot)
.filter(p -> p.toString().endsWith(".h"))
.forEach(p -> {
try {
String original = new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
String patched = original.replaceAll("(get_static_[A-Za-z0-9_]+)\\(\\);", "$1(CODENAME_ONE_THREAD_STATE);");
if (!original.equals(patched)) {
Files.write(p, patched.getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}

static void writeRuntimeStubs(Path srcRoot) throws IOException {
Path objectHeader = srcRoot.resolve("java_lang_Object.h");
if (!Files.exists(objectHeader)) {
Expand Down
Loading
Loading