Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
XuJiandong committed Mar 5, 2024
1 parent 8bc4175 commit b4bd2c4
Show file tree
Hide file tree
Showing 33 changed files with 417 additions and 318 deletions.
5 changes: 5 additions & 0 deletions script/testdata/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
ColumnLimit: 120
SortIncludes: false
12 changes: 9 additions & 3 deletions script/testdata/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ ALL_BINS := jalr_zero \
load_is_even_into_global \
load_is_even_with_snapshot \
load_arithmetic \
debugger
debugger \
spawn_caller_strcat

ALL_LIBS := is_even.lib \
add1.lib sub1.lib mul2.lib div2.lib

all-bins: clean-bins $(ALL_BINS)
all-libs: clean-libs $(ALL_LIBS)
all-bins: $(ALL_BINS)
all-libs: $(ALL_LIBS)

bins-in-docker:
docker run --rm -v `pwd`:/code $(BIN_BUILDER_DOCKER) bash -c "cd /code && make all-bins"
Expand All @@ -73,6 +74,9 @@ clean-bins:
clean-libs:
-rm -f $(ALL_LIBS)

fmt:
clang-format -i *.c *.h

clean: clean-bins clean-libs

%: %.c
Expand Down Expand Up @@ -114,3 +118,5 @@ sub1.lib: sub1.c
mul2.lib: mul2.c
div2.lib: div2.c
load_arithmetic: load_arithmetic.c

spawn_caller_strcat: spawn_caller_strcat.c
4 changes: 1 addition & 3 deletions script/testdata/add1.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <stdint.h>

__attribute__((visibility("default"))) uint64_t apply (uint64_t num) {
return num + 1;
}
__attribute__((visibility("default"))) uint64_t apply(uint64_t num) { return num + 1; }
26 changes: 13 additions & 13 deletions script/testdata/cpop_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* - `cpop(num0) == num 1`
*/

#include "ckb_syscalls.h"
#include "blockchain.h"
#include "ckb_syscalls.h"

#ifdef DEBUG
#include <stdio.h>
Expand All @@ -17,25 +17,22 @@

#define SCRIPT_SIZE 32768

static uint64_t cpop (uint64_t rs1) {
static uint64_t cpop(uint64_t rs1) {
uint64_t rd;
asm volatile (
asm volatile(
"mv s2, %1\n"
// "cpop s2, s2\n"
".byte 0x13,0x19,0x29,0x60\n"
"mv %0, s2\n"
: "=r"(rd)
: "r"(rs1)
: "s2"
);
: "s2");
return rd;
}

uint64_t read_u64_le (const uint8_t *src) {
return *(const uint64_t *)src;
}
uint64_t read_u64_le(const uint8_t *src) { return *(const uint64_t *)src; }

int main (int argc, char *argv[]) {
int main(int argc, char *argv[]) {
int ret;
uint64_t len = SCRIPT_SIZE;
uint8_t script[SCRIPT_SIZE];
Expand Down Expand Up @@ -67,17 +64,20 @@ int main (int argc, char *argv[]) {
}

volatile uint64_t num0 = read_u64_le(bytes_seg.ptr);
volatile uint64_t num1 = read_u64_le(bytes_seg.ptr+8);
volatile uint64_t num1 = read_u64_le(bytes_seg.ptr + 8);

sprintf(message, "num0 = %ld", num0); ckb_debug(message);
sprintf(message, "num1 = %ld", num1); ckb_debug(message);
sprintf(message, "num0 = %ld", num0);
ckb_debug(message);
sprintf(message, "num1 = %ld", num1);
ckb_debug(message);

if (num0 == 0 && num1 == 0) {
return CKB_SUCCESS;
}

volatile uint64_t num1_actual = cpop(num0);
sprintf(message, "cpop(%lx) = %ld (actual) == %ld (expected)", num0, num1_actual, num1); ckb_debug(message);
sprintf(message, "cpop(%lx) = %ld (actual) == %ld (expected)", num0, num1_actual, num1);
ckb_debug(message);

if (num1 != num1_actual) {
return -5;
Expand Down
6 changes: 2 additions & 4 deletions script/testdata/current_cycles.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include "ckb_syscalls.h"

int current_cycles() {
return syscall(2042, 0, 0, 0, 0, 0, 0);
}
int current_cycles() { return syscall(2042, 0, 0, 0, 0, 0, 0); }

int main() {
int prev = current_cycles();
int curr;
for (int i=0; i<4096; i++) {
for (int i = 0; i < 4096; i++) {
curr = current_cycles();
if (curr <= prev) {
return -1;
Expand Down
13 changes: 5 additions & 8 deletions script/testdata/current_cycles_with_snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
#define sprintf(...)
#endif

void try_pause() {
syscall(2178, 0, 0, 0, 0, 0, 0);
}
void try_pause() { syscall(2178, 0, 0, 0, 0, 0, 0); }

int current_cycles() {
return syscall(2042, 0, 0, 0, 0, 0, 0);
}
int current_cycles() { return syscall(2042, 0, 0, 0, 0, 0, 0); }

int main() {
#ifdef DEBUG
char message[2048];
#endif
int prev = current_cycles();
int curr;
for (int i=0; i<4096; i++) {
for (int i = 0; i < 4096; i++) {
curr = current_cycles();
sprintf(message, "prev = %d, curr = %d", prev, curr); ckb_debug(message);
sprintf(message, "prev = %d, curr = %d", prev, curr);
ckb_debug(message);
if (i > 16) {
try_pause();
}
Expand Down
3 changes: 2 additions & 1 deletion script/testdata/debugger.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ckb_syscalls.h"
#include <stdio.h>

#include "ckb_syscalls.h"

int main() {
char message[2048];
sprintf(message, "debugger print utf-8 string");
Expand Down
4 changes: 1 addition & 3 deletions script/testdata/div2.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <stdint.h>

__attribute__((visibility("default"))) uint64_t apply (uint64_t num) {
return num / 2;
}
__attribute__((visibility("default"))) uint64_t apply(uint64_t num) { return num / 2; }
26 changes: 13 additions & 13 deletions script/testdata/exec_callee.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
int main(int argc, char* argv[]) {
if (argc != 3) {
return 1;
}
if (argv[0][0] != 'a') {
return 2;
}
if (argv[1][0] != 'b') {
return 3;
}
if (argv[2][0] != 'c') {
return 4;
}
return 0;
if (argc != 3) {
return 1;
}
if (argv[0][0] != 'a') {
return 2;
}
if (argv[1][0] != 'b') {
return 3;
}
if (argv[2][0] != 'c') {
return 4;
}
return 0;
}
2 changes: 1 addition & 1 deletion script/testdata/exec_caller_big_offset_length.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ int main() {
char *argv[] = {"a", "b", "c"};
int ret = syscall(2043, 1, 3, 0, 0xffffffffffffffff, argc, argv);
if (ret != 0) {
return ret;
return ret;
}
return -1;
}
Loading

0 comments on commit b4bd2c4

Please sign in to comment.