Skip to content

Commit

Permalink
refactor!: use lowercase for all types in C API
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 25, 2024
1 parent 6787bb0 commit 3acb141
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/clua-i-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static int machine_obj_index_read_word(lua_State *L) {
static int machine_obj_index_run(lua_State *L) {
auto &m = clua_check<clua_managed_cm_ptr<cm_machine>>(L, 1);
const uint64_t mcycle_end = luaL_optinteger(L, 2, UINT64_MAX);
CM_BREAK_REASON break_reason = CM_BREAK_REASON_FAILED;
cm_break_reason break_reason = CM_BREAK_REASON_FAILED;
if (cm_run(m.get(), mcycle_end, &break_reason) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
Expand Down Expand Up @@ -281,7 +281,7 @@ static int machine_obj_index_log_reset_uarch(lua_State *L) {
static int machine_obj_index_run_uarch(lua_State *L) {
auto &m = clua_check<clua_managed_cm_ptr<cm_machine>>(L, 1);
const uint64_t cycle_end = luaL_optinteger(L, 2, UINT64_MAX);
CM_UARCH_BREAK_REASON status = CM_UARCH_BREAK_REASON_REACHED_TARGET_CYCLE;
cm_uarch_break_reason status = CM_UARCH_BREAK_REASON_REACHED_TARGET_CYCLE;
if (cm_run_uarch(m.get(), cycle_end, &status) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
Expand Down
2 changes: 1 addition & 1 deletion src/clua-jsonrpc-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static int jsonrpc_machine_class_get_csr_address(lua_State *L) {
auto &managed_jsonrpc_mgr =
clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, lua_upvalueindex(1), lua_upvalueindex(2));
uint64_t csr_address{};
const CM_CSR csr = clua_check_cm_proc_csr(L, 1);
const cm_csr csr = clua_check_cm_proc_csr(L, 1);
if (cm_jsonrpc_get_csr_address(managed_jsonrpc_mgr.get(), csr, &csr_address) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
Expand Down
4 changes: 2 additions & 2 deletions src/clua-machine-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ void clua_delete<nlohmann::json>(nlohmann::json *ptr) {
delete ptr;
}

CM_CSR clua_check_cm_proc_csr(lua_State *L, int idx) try {
cm_csr clua_check_cm_proc_csr(lua_State *L, int idx) try {
/// \brief Mapping between CSR names and C API constants
const static std::unordered_map<std::string, CM_CSR> g_cm_proc_csr_name = {
const static std::unordered_map<std::string, cm_csr> g_cm_proc_csr_name = {
// clang-format off
{"x0", CM_CSR_X0},
{"x1", CM_CSR_X1},
Expand Down
2 changes: 1 addition & 1 deletion src/clua-machine-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ T *clua_push_new_managed_toclose_ptr(lua_State *L, T &&value, int ctxidx = lua_u
/// \param L Lua state
/// \param idx Index in stack
/// \returns C API CSR selector. Lua argument error if unknown
CM_CSR clua_check_cm_proc_csr(lua_State *L, int idx);
cm_csr clua_check_cm_proc_csr(lua_State *L, int idx);

/// \brief Pushes a C api hash object to the Lua stack
/// \param L Lua state
Expand Down
2 changes: 1 addition & 1 deletion src/jsonrpc-machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ int32_t cm_jsonrpc_rebind(const cm_jsonrpc_mgr *mgr, const char *address, const
return cm_result_failure();
}

int32_t cm_jsonrpc_get_csr_address(const cm_jsonrpc_mgr *mgr, CM_CSR csr, uint64_t *val) try {
int32_t cm_jsonrpc_get_csr_address(const cm_jsonrpc_mgr *mgr, cm_csr csr, uint64_t *val) try {
if (val == nullptr) {
throw std::invalid_argument("invalid val output");
}
Expand Down
2 changes: 1 addition & 1 deletion src/jsonrpc-machine-c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CM_API int32_t cm_jsonrpc_get_default_config(const cm_jsonrpc_mgr *mgr, const ch
/// \param csr The CSR.
/// \param val Receives address of the CSR.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_jsonrpc_get_csr_address(const cm_jsonrpc_mgr *mgr, CM_CSR csr, uint64_t *val);
CM_API int32_t cm_jsonrpc_get_csr_address(const cm_jsonrpc_mgr *mgr, cm_csr csr, uint64_t *val);

// -------------------------------------
// Machine API functions
Expand Down
14 changes: 7 additions & 7 deletions src/machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ int32_t cm_store(cm_machine *m, const char *dir) try {
return cm_result_failure();
}

int32_t cm_run(cm_machine *m, uint64_t mcycle_end, CM_BREAK_REASON *break_reason) try {
int32_t cm_run(cm_machine *m, uint64_t mcycle_end, cm_break_reason *break_reason) try {
auto *cpp_machine = convert_from_c(m);
const auto status = cpp_machine->run(mcycle_end);
if (break_reason) {
*break_reason = static_cast<CM_BREAK_REASON>(status);
*break_reason = static_cast<cm_break_reason>(status);
}
return cm_result_success();
} catch (...) {
Expand Down Expand Up @@ -249,11 +249,11 @@ int32_t cm_log_reset_uarch(cm_machine *m, int32_t log_type, bool one_based, cons
return cm_result_failure();
}

int32_t cm_run_uarch(cm_machine *m, uint64_t uarch_cycle_end, CM_UARCH_BREAK_REASON *uarch_break_reason) try {
int32_t cm_run_uarch(cm_machine *m, uint64_t uarch_cycle_end, cm_uarch_break_reason *uarch_break_reason) try {
auto *cpp_machine = convert_from_c(m);
const auto status = cpp_machine->run_uarch(uarch_cycle_end);
if (uarch_break_reason) {
*uarch_break_reason = static_cast<CM_UARCH_BREAK_REASON>(status);
*uarch_break_reason = static_cast<cm_uarch_break_reason>(status);
}
return cm_result_success();
} catch (...) {
Expand Down Expand Up @@ -353,7 +353,7 @@ int32_t cm_verify_merkle_tree(cm_machine *m, bool *result) try {
return cm_result_failure();
}

int32_t cm_read_csr(const cm_machine *m, CM_CSR csr, uint64_t *val) try {
int32_t cm_read_csr(const cm_machine *m, cm_csr csr, uint64_t *val) try {
if (val == nullptr) {
throw std::invalid_argument("invalid val output");
}
Expand All @@ -365,7 +365,7 @@ int32_t cm_read_csr(const cm_machine *m, CM_CSR csr, uint64_t *val) try {
return cm_result_failure();
}

int32_t cm_write_csr(cm_machine *m, CM_CSR csr, uint64_t val) try {
int32_t cm_write_csr(cm_machine *m, cm_csr csr, uint64_t val) try {
auto *cpp_machine = convert_from_c(m);
auto cpp_csr = static_cast<cartesi::machine::csr>(csr);
cpp_machine->write_csr(cpp_csr, val);
Expand All @@ -374,7 +374,7 @@ int32_t cm_write_csr(cm_machine *m, CM_CSR csr, uint64_t val) try {
return cm_result_failure();
}

uint64_t cm_get_csr_address(CM_CSR csr) try {
uint64_t cm_get_csr_address(cm_csr csr) try {
auto cpp_csr = static_cast<cartesi::machine::csr>(csr);
uint64_t address = cartesi::machine::get_csr_address(cpp_csr);
cm_result_success();
Expand Down
36 changes: 18 additions & 18 deletions src/machine-c-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ extern "C" {
// -----------------------------------------------------------------------------

/// \brief Constants.
typedef enum CM_CONSTANT {
typedef enum cm_constant {
CM_HASH_SIZE = 32,
CM_TREE_LOG2_WORD_SIZE = 5,
CM_TREE_LOG2_PAGE_SIZE = 12,
CM_TREE_LOG2_ROOT_SIZE = 64,
} CM_CONSTANT;
} cm_constant;

/// \brief Error codes returned from the C API.
typedef enum CM_ERROR {
typedef enum cm_error {
CM_ERROR_OK,
CM_ERROR_INVALID_ARGUMENT,
CM_ERROR_DOMAIN_ERROR,
Expand All @@ -76,33 +76,33 @@ typedef enum CM_ERROR {
CM_ERROR_BAD_VARIANT_ACCESS,
CM_ERROR_EXCEPTION,
CM_ERROR_UNKNOWN,
} CM_ERROR;
} cm_error;

/// \brief Reasons for a machine run interruption.
typedef enum CM_BREAK_REASON {
typedef enum cm_break_reason {
CM_BREAK_REASON_FAILED,
CM_BREAK_REASON_HALTED,
CM_BREAK_REASON_YIELDED_MANUALLY,
CM_BREAK_REASON_YIELDED_AUTOMATICALLY,
CM_BREAK_REASON_YIELDED_SOFTLY,
CM_BREAK_REASON_REACHED_TARGET_MCYCLE,
} CM_BREAK_REASON;
} cm_break_reason;

/// \brief Reasons for a machine microarchitecture run interruption.
typedef enum {
typedef enum cm_uarch_break_reason {
CM_UARCH_BREAK_REASON_REACHED_TARGET_CYCLE,
CM_UARCH_BREAK_REASON_UARCH_HALTED,
} CM_UARCH_BREAK_REASON;
} cm_uarch_break_reason;

/// \brief Access log types.
typedef enum CM_ACCESS_LOG_TYPE {
typedef enum cm_access_log_type {
CM_ACCESS_LOG_TYPE_PROOFS = 1, ///< Includes proofs
CM_ACCESS_LOG_TYPE_ANNOTATIONS = 2, ///< Includes annotations
CM_ACCESS_LOG_TYPE_LARGE_DATA = 4, ///< Includes data bigger than 8 bytes
} CM_ACCESS_LOG_TYPE;
} cm_access_log_type;

/// \brief Machine control and status registers.
typedef enum CM_CSR {
typedef enum cm_csr {
// Processor CSRs
CM_CSR_X0,
CM_CSR_X1,
Expand Down Expand Up @@ -260,7 +260,7 @@ typedef enum CM_CSR {
CM_CSR_HTIF_FROMHOST_REASON,
CM_CSR_HTIF_FROMHOST_DATA,
CM_CSR_UNKNOWN,
} CM_CSR;
} cm_csr;

/// \brief Machine hash array.
typedef uint8_t cm_hash[CM_HASH_SIZE];
Expand All @@ -276,7 +276,7 @@ typedef struct cm_machine cm_machine;
/// \brief Returns the error message set by the very last C API call.
/// \returns A C string, remains valid until next C API call.
/// \details The string returned by this function must not be changed nor deallocated,
/// and remains valid until next C API function that can return a CM_ERROR code is called.
/// and remains valid until next C API function that can return a cm_error code is called.
/// In case the last call was successful it returns an empty string.
/// It uses a thread local variable, so it's safe to call from different threads.
CM_API const char *cm_get_last_error_message();
Expand All @@ -293,7 +293,7 @@ CM_API const char *cm_get_default_config();
/// \param csr The CSR.
/// \returns The address of the specified CSR.
/// In case the CSR is invalid, UINT64_MAX is returned and last error message is set.
CM_API uint64_t cm_get_csr_address(CM_CSR csr);
CM_API uint64_t cm_get_csr_address(cm_csr csr);

// ---------------------------------
// Machine API functions
Expand Down Expand Up @@ -395,14 +395,14 @@ CM_API int32_t cm_read_word(const cm_machine *m, uint64_t address, uint64_t *val
/// \param csr CSR to read.
/// \param val Receives the value.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_read_csr(const cm_machine *m, CM_CSR csr, uint64_t *val);
CM_API int32_t cm_read_csr(const cm_machine *m, cm_csr csr, uint64_t *val);

/// \brief Writes the value of a CSR.
/// \param m Pointer to valid machine instance.
/// \param csr CSR to write.
/// \param val Value to write.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_write_csr(cm_machine *m, CM_CSR csr, uint64_t val);
CM_API int32_t cm_write_csr(cm_machine *m, cm_csr csr, uint64_t val);

/// \brief Reads a chunk of data from the machine memory.
/// \param m Pointer to valid machine instance.
Expand Down Expand Up @@ -524,15 +524,15 @@ CM_API int32_t cm_rollback(cm_machine *m);
/// \param mcycle_end End cycle value.
/// \param break_reason Receives reason for machine run interruption when not NULL.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_run(cm_machine *m, uint64_t mcycle_end, CM_BREAK_REASON *break_reason);
CM_API int32_t cm_run(cm_machine *m, uint64_t mcycle_end, cm_break_reason *break_reason);

/// \brief Runs the machine in the microarchitecture until the mcycle advances by one unit
/// or the micro cycles counter reaches uarch_cycle_end.
/// \param m Pointer to valid machine instance.
/// \param uarch_cycle_end End micro cycle value.
/// \param uarch_break_reason Receives reason for machine microarchitecture run interruption when not NULL.
/// \returns 0 for success, non zero code for error.
CM_API int32_t cm_run_uarch(cm_machine *m, uint64_t uarch_cycle_end, CM_UARCH_BREAK_REASON *uarch_break_reason);
CM_API int32_t cm_run_uarch(cm_machine *m, uint64_t uarch_cycle_end, cm_uarch_break_reason *uarch_break_reason);

/// \brief Resets the entire microarchitecture state to pristine values.
/// \param m Pointer to valid machine instance.
Expand Down
2 changes: 1 addition & 1 deletion tests/misc/test-machine-c-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,7 @@ BOOST_FIXTURE_TEST_CASE_NOLINT(step_hash_test, access_log_machine_fixture) {
}

BOOST_AUTO_TEST_CASE_NOLINT(machine_run_null_machine_test) {
CM_BREAK_REASON break_reason{};
cm_break_reason break_reason{};
int error_code = cm_run(nullptr, 1000, &break_reason);
BOOST_REQUIRE_EQUAL(error_code, CM_ERROR_INVALID_ARGUMENT);
BOOST_CHECK_EQUAL(break_reason, CM_BREAK_REASON_FAILED);
Expand Down

0 comments on commit 3acb141

Please sign in to comment.