Commit aaf91e73 authored by Jamie A. Jennings's avatar Jamie A. Jennings

Updated util/dis.c to work with print_instructions from the new print.c

parent 1d15c452
/* -*- Mode: C; -*- */
/* */
/* print.h */
/* */
/* © Copyright Jamie A. Jennings 2018. */
/* LICENSE: MIT License (https://opensource.org/licenses/mit-license.html) */
/* AUTHOR: Jamie A. Jennings */
void walk_instructions (Instruction *p,
int codesize,
void *(*operation)(const Instruction *, Instruction *, void *),
void *context);
void print_ktable (Ktable *kt);
void *print_instruction (const Instruction *op, Instruction *p, void *context);
void print_instructions (Instruction *p, int codesize);
......@@ -8,15 +8,11 @@
/* AUTHOR: Jamie A. Jennings */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "config.h"
#include "vm.h"
#include "file.h"
#include "ktable.h"
#include "rplx.h"
#include "print.h"
extern int sizei(const Instruction *p);
......@@ -51,118 +47,13 @@ static const char *strerror_error(const char *filename, int line, int code) {
/* ------------------------------------------------------------------------------------------------------------- */
static void print_ktable (Ktable *kt) {
int n, i;
if (!kt) return; /* no ktable? */
n = (int) ktable_len(kt);
for (i = 1; i <= n; i++) {
printf("%4d = %s\n", i, ktable_element_name(kt, i));
}
}
static void printcharset (const byte *st) {
int i;
printf("[");
for (i = 0; i <= UCHAR_MAX; i++) {
int first = i;
while (testchar(st, i) && i <= UCHAR_MAX) i++;
if (i - 1 == first) /* unary range? */
printf("(%02x)", first);
else if (i - 1 > first) /* non-empty range? */
printf("(%02x-%02x)", first, i - 1);
}
printf("]");
}
static void printcapkind (int kind) {
const char *const modes[] = {
"close", "position", "backref", "simple",
"named", "constant", "final"};
printf("%s", modes[kind]);
}
static void printjmp_absolute (const Instruction *op, const Instruction *p) {
printf("-> %d", (int)(p + (p + 1)->offset - op));
}
/* Print instructions with their absolute addresses, showing jump
* destinations also as absolute addresses.
*/
static void *printinst_absolute (const Instruction *op, const Instruction *p, void *context) {
UNUSED(context);
/* TODO: call decode here... and change code below to use opcode(inst), ichar(inst), etc. */
printf("%4ld %s ", (long)(p - op), OPCODE_NAME(p->i.code));
switch ((Opcode)p->i.code) {
case IChar: {
printf("'%c'", p->i.aux);
break;
}
case ITestChar: {
printf("'%c'", p->i.aux); printjmp_absolute(op, p);
break;
}
case IOpenCapture: {
printcapkind(addr(p));
printf("/#%d", p->i.aux);
break;
}
case ISet: {
printcharset((p+1)->buff);
break;
}
case ITestSet: {
printcharset((p+2)->buff); printjmp_absolute(op, p);
break;
}
case ISpan: {
printcharset((p+1)->buff);
break;
}
case IOpenCall: {
printf("-> %d", (p + 1)->offset);
break;
}
case IBehind: {
printf("%d", p->i.aux);
break;
}
case IJmp: case ICall: case ICommit: case IChoice:
case IPartialCommit: case IBackCommit: case ITestAny: {
printjmp_absolute(op, p);
break;
}
default: break;
}
printf("\n");
return NULL;
}
static void walk_instructions (size_t codesize,
Instruction *p,
void *(*operation)(const Instruction *, const Instruction *, void *),
void *context) {
Instruction *op = p;
int n = (int) codesize;
while (p < op + n) {
(*operation)(op, p, context);
p += sizei(p);
}
}
static void print_instructions_absolute (size_t codesize, Instruction *p) {
walk_instructions(codesize, p, &printinst_absolute, NULL);
}
static void print_instructions (size_t codesize, Instruction *p) {
print_instructions_absolute(codesize, p);
}
static int ktable_dups(Ktable *kt, int *distinct_dups, int *unique_elements) {
Ktable_element *elements = ktable_sorted_index(kt);
/* Now count the duplicates. */
int dups = 0, distinct = 0, new = 0, unique = 1;
for (int i = 1; i < ktable_len(kt); i++) {
if (ktable_entry_name_compare(elements[i-1], elements[i]) == 0) {
if (ktable_entry_name_compare(kt, &elements[i-1], &elements[i]) == 0) {
if (new == 1) { distinct++; new = 0; }
dups++;
} else {
......@@ -190,16 +81,16 @@ int main(int argc, char **argv) {
while ((flag = getopt(argc, argv, "kisc")) != -1)
switch (flag) {
case 'k':
case 'k': /* print ktable (symbol table) */
kflag = 1;
break;
case 'i':
case 'i': /* print instruction vector */
iflag = 1;
break;
case 's':
case 's': /* print summary */
sflag = 1;
break;
case 'c': /* compact ktable (for testing) */
case 'c': /* compact the ktable (for testing) */
cflag = 1;
break;
case '?':
......@@ -229,7 +120,7 @@ int main(int argc, char **argv) {
}
if (iflag) {
printf("Code:\n");
print_instructions(c.codesize, c.code);
print_instructions(c.code, c.codesize);
printf("\n");
}
if (sflag) {
......@@ -244,11 +135,15 @@ int main(int argc, char **argv) {
printf("\n");
}
if (cflag) {
const char *element_name;
size_t element_len;
int newidx;
Ktable *ckt = ktable_compact(c.ktable);
printf("Compacted ktable:\n");
print_ktable(ckt);
for (int idx = 1; idx <= ktable_len(c.ktable); idx++) {
int newidx = ktable_compact_search(ckt, ktable_element(c.ktable, idx));
element_name = ktable_element_name(c.ktable, idx, &element_len);
newidx = ktable_compact_search(ckt, element_name, element_len);
if (newidx == 0) printf("*** ERROR: ");
printf("%4d --> %4d\n", idx, newidx);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment