Skip to content

Commit

Permalink
Fix load-global-var bytecode. (#1377)
Browse files Browse the repository at this point in the history
The wide variant wasn't immediately following the non-wide one.
  • Loading branch information
floitsch authored Jan 25, 2023
1 parent 2e2d9dc commit d8c9f39
Show file tree
Hide file tree
Showing 33 changed files with 355 additions and 203 deletions.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ if (DEFINED ENV{TOIT_CHECK_PROPAGATED_TYPES})
set(TOIT_INTERPRETER_FLAGS "${TOIT_INTERPRETER_FLAGS};-DTOIT_CHECK_PROPAGATED_TYPES")
endif()

set_source_files_properties(interpreter_core.cc PROPERTIES COMPILE_OPTIONS "-O3")
set_source_files_properties(interpreter_core.cc PROPERTIES COMPILE_OPTIONS "-O3;$ENV{LOCAL_INTERPRETER_CXXFLAGS}")
set_source_files_properties(interpreter_run.cc PROPERTIES COMPILE_OPTIONS "-O3;${TOIT_INTERPRETER_FLAGS};$ENV{LOCAL_INTERPRETER_CXXFLAGS}")
set_source_files_properties(utils.cc PROPERTIES COMPILE_FLAGS "-DTOIT_MODEL=\"\\\"${TOIT_MODEL}\\\"\" -DVM_GIT_INFO=\"\\\"${VM_GIT_INFO}\\\"\" -DVM_GIT_VERSION=\"\\\"${TOIT_GIT_VERSION}\\\"\"")

Expand Down
4 changes: 2 additions & 2 deletions src/bytecodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,10 @@ enum BytecodeFormat {
BYTECODE(LOAD_METHOD, 5, OP_WU, "load method") \
\
BYTECODE(LOAD_GLOBAL_VAR, 2, OP_BG, "load global var") \
BYTECODE(LOAD_GLOBAL_VAR_DYNAMIC, 1, OP, "load global var dynamic") \
BYTECODE(LOAD_GLOBAL_VAR_WIDE, 3, OP_SG, "load global var wide") \
BYTECODE(LOAD_GLOBAL_VAR_LAZY, 2, OP_BG, "load global var lazy") \
BYTECODE(LOAD_GLOBAL_VAR_LAZY_WIDE, 3, OP_SG, "load global var lazy wide") \
BYTECODE(LOAD_GLOBAL_VAR_DYNAMIC, 1, OP, "load global var dynamic") \
BYTECODE(STORE_GLOBAL_VAR, 2, OP_BG, "store global var") \
BYTECODE(STORE_GLOBAL_VAR_WIDE, 3, OP_SG, "store global var wide") \
BYTECODE(STORE_GLOBAL_VAR_DYNAMIC, 1, OP, "store global var dynamic") \
Expand All @@ -144,7 +144,7 @@ enum BytecodeFormat {
BYTECODE(AS_CLASS_WIDE, 3, OP_SCI, "as class wide") \
BYTECODE(AS_INTERFACE, 2, OP_BII, "as interface") \
BYTECODE(AS_INTERFACE_WIDE, 3, OP_SII, "as interface wide") \
BYTECODE(AS_LOCAL, 2, OP_BLC, "load local, as, pop") \
BYTECODE(AS_LOCAL, 2, OP_BLC, "load local, as class, pop") \
\
BYTECODE(INVOKE_STATIC, 3, OP_SD, "invoke static") \
BYTECODE(INVOKE_STATIC_TAIL, 5, OP_SD_BS_BU, "invoke static tail") \
Expand Down
46 changes: 46 additions & 0 deletions tests/ctest/bytecode_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2023 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/LICENSE file.

#include <string>

#include "../../src/top.h"
#include "../../src/bytecodes.h"

namespace toit {

using namespace compiler;

#define BYTECODE_NAME(name, length, format, print) #name,
const char* ALL_BYTECODE_NAMES[] {
BYTECODES(BYTECODE_NAME) "ILLEGAL"
};
#undef BYTECODE_NAME

static bool ends_with(const std::string& str, const std::string& suffix) {
return str.size() >= suffix.size() &&
str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}

int main(int argc, char** argv) {
int count = sizeof(ALL_BYTECODE_NAMES) / sizeof(ALL_BYTECODE_NAMES[0]);
std::string last;
for (int i = 0; i < count; i++) {
auto current = std::string(ALL_BYTECODE_NAMES[i]);
if (ends_with(current, "WIDE")) {
printf("checking %s\n", current.c_str());
if (last + "_WIDE" != current) {
FATAL("WIDE bytecode must be non-wide + 1");
}
}
last = current;
}
return 0;
}

}

int main(int argc, char** argv) {
toit::throwing_new_allowed = true;
return toit::main(argc, argv);
}
28 changes: 25 additions & 3 deletions tests/toitp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,31 @@ ADD_TOIT_EXE(
${TOITP_DEP}
"")

add_custom_target(build_test_toitp_exe DEPENDS ${TOITP_EXE})
add_dependencies(check build_test_toitp_exe)
add_dependencies(check_slow build_test_toitp_exe)
add_executable(
bytecode_lister
bytecode_lister.cc
)

set(BYTECODE_LIST ${TOITP_TEST_DIR}/bytecode.output)

add_custom_target(build_test_toitp_requirements DEPENDS ${TOITP_EXE} ${BYTECODE_LIST})
add_dependencies(check build_test_toitp_requirements)
add_dependencies(check_slow build_test_toitp_requirements)

add_custom_command(
OUTPUT ${BYTECODE_LIST}
COMMAND $<TARGET_FILE:bytecode_lister> > ${BYTECODE_LIST}
DEPENDS $<TARGET_FILE:bytecode_lister>
)

set(BYTECODE_MATCH_TEST ${CMAKE_CURRENT_SOURCE_DIR}/bytecode_match_test.toit)
file(RELATIVE_PATH BYTECODE_MATCH_TEST_NAME ${TOIT_SDK_SOURCE_DIR} ${BYTECODE_MATCH_TEST})
set(BYTECODE_MATCH_TEST_NAME tests-toitp-bytecode_match_test)
add_test(
NAME ${BYTECODE_MATCH_TEST_NAME}
COMMAND $<TARGET_FILE:toit.run> ${BYTECODE_MATCH_TEST} ${BYTECODE_LIST}
DEPENDS ${BYTECODE_LIST}
)

foreach(file ${TOITP_TESTS})
get_filename_component(base ${file} NAME_WE)
Expand Down
46 changes: 46 additions & 0 deletions tests/toitp/bytecode_lister.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (C) 2023 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/LICENSE file.

#include <string>
#include <stdio.h>

#include "../../src/bytecodes.h"

namespace toit {

using namespace compiler;

struct Bytecode {
const char* name;
int length;
const char* format;
const char* print;
};

#define BYTECODE_STRUCT(name_, length_, format_, print_) \
{ .name=#name_, .length=length_, .format=#format_, .print=print_ },
const Bytecode ALL_BYTECODES[] {
BYTECODES(BYTECODE_STRUCT)
{ .name="ILLEGAL", .length=0, .format="", .print="" }
};
#undef BYTECODE_STRUCT

int main(int argc, char** argv) {
int count = sizeof(ALL_BYTECODES) / sizeof(ALL_BYTECODES[0]);
// Don't print the illegal one.
for (int i = 0; i < count - 1; i++) {
printf("%s %d %s %s\n",
ALL_BYTECODES[i].name,
ALL_BYTECODES[i].length,
ALL_BYTECODES[i].format,
ALL_BYTECODES[i].print);
}
return 0;
}

}

int main(int argc, char** argv) {
return toit::main(argc, argv);
}
38 changes: 38 additions & 0 deletions tests/toitp/bytecode_match_test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (C) 2023 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/LICENSE file.
import expect show *
import host.file
import ...tools.snapshot

parse_bytecodes bytes:
str := bytes.to_string
lines := str.split "\n"
lines.map --in_place: it.trim
lines.filter --in_place: it != ""
return lines.map: | line/string |
space := line.index_of " "
name := line[..space]
size := int.parse line[space + 1..space + 2]
format_space := line.index_of " " (space + 3)
format := line[space + 3..format_space]
description := line[format_space + 1..]
[name, size, format, description]

main args:
c_bytecode_list := parse_bytecodes (file.read_content args[0])
expect_equals BYTE_CODES.size c_bytecode_list.size
used_c_formats := {}
format_mapping := {:}
BYTE_CODES.size.repeat:
toit_bytecode /Bytecode := BYTE_CODES[it]
c_bytecode := c_bytecode_list[it]
if not format_mapping.contains toit_bytecode.format:
format_mapping[toit_bytecode.format] = c_bytecode[2]
expect_not (used_c_formats.contains c_bytecode[2])
used_c_formats.add c_bytecode[2]
expect_equals toit_bytecode.name c_bytecode[0]
expect_equals toit_bytecode.size c_bytecode[1]
expect_equals format_mapping[toit_bytecode.format] c_bytecode[2]
expect_equals toit_bytecode.description c_bytecode[3]
2 changes: 1 addition & 1 deletion tests/type_propagation/gold/array_do_test.gold
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ main tests/type_propagation/array_do_test.toit
0[025] - load smi 1
1[053] - invoke static create_array_ <sdk>/core/collections.toit // [{SmallInteger_}] -> {LargeArray_|SmallArray_}
4[053] - invoke static create_list_literal_from_array_ <sdk>/core/collections.toit // [{LargeArray_|SmallArray_}] -> {List_}
7[029] - load [block] in main tests/type_propagation/array_do_test.toit
7[029] - load method [block] in main tests/type_propagation/array_do_test.toit
12[015] - load local 1
13[038] - load block 1
15[058] - invoke virtual do // [{List_}, [block]] -> {Null_}
Expand Down
2 changes: 1 addition & 1 deletion tests/type_propagation/gold/array_do_test.gold-O2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ main tests/type_propagation/array_do_test.toit
0[025] - load smi 1
1[053] - invoke static create_array_ <sdk>/core/collections.toit // [{SmallInteger_}] -> {LargeArray_|SmallArray_}
4[053] - invoke static create_list_literal_from_array_ <sdk>/core/collections.toit // [{LargeArray_|SmallArray_}] -> {List_}
7[029] - load [block] in main tests/type_propagation/array_do_test.toit
7[029] - load method [block] in main tests/type_propagation/array_do_test.toit
12[015] - load local 1
13[038] - load block 1
15[058] - invoke virtual do // [{List_}, [block]] -> {Null_}
Expand Down
Loading

0 comments on commit d8c9f39

Please sign in to comment.