Skip to content

Commit 79c7335

Browse files
committed
some changed i have locally
1 parent 620a4d2 commit 79c7335

File tree

7 files changed

+30
-33
lines changed

7 files changed

+30
-33
lines changed

kisscomp.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ static int finallize_jumppair(struct compstate* x, uint16_t f, uint16_t s) {
8989
uint16_t start = f + sizeof(uint16_t) + 1;
9090
uint16_t end = s + sizeof(uint16_t) + 1;
9191
// set jump distanations
92-
*(uint16_t*)(x->bytecode + f + 1) = end;
93-
*(uint16_t*)(x->bytecode + s + 1) = start;
92+
*(uint16_t*)(x->bytecode + f + 1) = end - start + 2;
93+
*(uint16_t*)(x->bytecode + s + 1) = start - end + 2;
9494

9595
// do some optimisations
9696
loopoptimisation(x, f, s);
@@ -175,7 +175,7 @@ static void newcode(struct compstate* x, enum bytecode n) {
175175
}
176176

177177
/*
178-
* Compilation functions :p
178+
* Compilation function :p
179179
*/
180180
int loadcode(struct kissfuck* kf, const char* filename) {
181181
if (kf == ERR_ERR) return ERR_ERR;

kissdbg.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void printbytecode (struct kissfuck* x, int i, int arg) {
6060
int val = *(uint16_t*)(x->bytecode + i + 1);
6161
fprintf(stderr, " %05i | ", val);
6262
} else if (arg == 3) { // JPZ and JPNZ
63-
get_label(i + 3); // save next instruction as
64-
int val = *(uint16_t*)(x->bytecode + i + 1);
63+
get_label(i + 3); // save next instruction position
64+
int val = (uint16_t)i + *(uint16_t*)(x->bytecode + i + 1);
6565
val = get_label(val);
6666
if (val < 0) fprintf(stderr, " ??? | ");
6767
else fprintf(stderr, " L%03i | ", val);

kissfuck.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct kissfuck {
9393
};
9494

9595
struct kissfuck* makectx();
96-
int loadcode(struct kissfuck*, const char* filename);
96+
int loadcode(struct kissfuck* restrict const, const char* filename);
9797
int execcode(struct kissfuck*);
9898
void stopcode(struct kissfuck*);
9999
int dumpcode(struct kissfuck*);

kissvm.c

+18-12
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ static void handler(int) {
3030
}
3131

3232
#define cell() (x->array[SP])
33-
#define reads() ((uint8_t)x->bytecode[IP])
34-
#define readl() *(uint16_t*)(x->bytecode + (IP))
33+
#define reads() ((uint8_t)BC[IP])
34+
#define readl() *(uint16_t*)(BC + (IP))
3535

36-
int execcode (struct kissfuck* const x) {
36+
int execcode (struct kissfuck* restrict const x) {
3737
signal(SIGINT, handler);
3838

39-
int works = 1;
4039
register uint16_t IP = x->instp;
4140
register uint16_t SP = x->cellp;
41+
register const uint8_t *BC = x->bytecode;
4242
has_signal = 0;
4343

44-
while (works) {
44+
while (BC) {
4545
if (unlikely(has_signal)) {
4646
fprintf(stderr, "\n[vm] : INTERRUPTED!\n");
4747
break;
@@ -50,7 +50,7 @@ int execcode (struct kissfuck* const x) {
5050
IP++;
5151
switch (C) {
5252
case BC_HALT:
53-
fflush(stdout); works = 0; IP--;
53+
fflush(stdout); BC = NULL; IP--;
5454
break;
5555
case BC_SET :
5656
cell() = reads();
@@ -62,33 +62,39 @@ int execcode (struct kissfuck* const x) {
6262
break;
6363
case BC_IN :
6464
// ignore other characters
65-
for (int i = 0; i < (reads() - 1); i++) getchar();
65+
uint8_t m = reads();
66+
for (int i = 0; i < (m - 1); i++) getchar();
6667
cell() = getchar();
6768
IP++;
6869
break;
6970
case BC_OUT :
70-
for (int i = 0; i < reads(); i++) putchar(cell());
71+
m = reads();
72+
for (int i = 0; i < m; i++) putchar(cell());
7173
IP++;
7274
break;
7375
case BC_NEXT:
7476
SP += readl();
7577
IP += 2;
7678
break;
7779
case BC_JPZ :
78-
if (!cell()) IP = readl();
80+
if (!cell()) IP += readl();
7981
else IP += 2; // skip readed value only if we skips jump
8082
break;
8183
case BC_JPNZ:
82-
if (cell()) IP = readl();
84+
if (cell()) IP += readl();
8385
else IP += 2; // skip readed value only if we skips jump
8486
break;
85-
case BC_NUNZ:
86-
while (cell()) SP += readl();
87+
case BC_NUNZ: {
88+
uint16_t cnt = readl();
89+
while (cell()) SP += cnt;
8790
IP += 2;
91+
}
8892
break;
8993
default :
9094
// you will never get here!
9195
unreachable();
96+
//fprintf(stderr, "BAD INSTRUCTION! pos = 0x%05X\n", IP);
97+
//return ERR_ERR;
9298
break;
9399
};
94100
};

makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
1313
DEPS := $(OBJS:.o=.d)
1414
INC_FLAGS := -I.
1515
#forming compiler flags
16-
UNIFLAGS ?= -O3 -Wall -Wextra -flto
16+
UNIFLAGS ?= -O3 -Wall -Wextra -flto -g
1717
LINFLAGS ?=
18-
CMPFLAGS ?= $(INC_FLAGS) -MMD -MP $(UNIFLAGS) -std=gnu2x -fanalyzer
18+
CMPFLAGS ?= $(INC_FLAGS) -MMD -MP $(UNIFLAGS) -std=gnu2x
1919

2020
#libraries, that we need to link
2121
LDLIBS := -lm

test.bfk

-13
This file was deleted.

tests/helloworld.bfk

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
++++++++
2+
[>+++++++++++++>++++<<-]>.
3+
---. +++++++. . +++. >.
4+
<++++++++. --------. +++. ------. --------.

0 commit comments

Comments
 (0)