Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

wmlsdasm.c File Reference

#include "wsint.h"
#include "gwlib/gwlib.h"
#include <sys/stat.h>

Include dependency graph for wmlsdasm.c:

Include dependency graph

Go to the source code of this file.

Functions

void usage (void)
const char * lookup_function (WsBc *bc, WsUInt8 index)
int main (int argc, char *argv[])

Variables

char * program


Function Documentation

const char * lookup_function WsBc bc,
WsUInt8  index
 

Definition at line 311 of file wmlsdasm.c.

References WsBcRec::function_names, WsBcFunctionNameRec::index, WsBcFunctionNameRec::name, WsBcRec::num_function_names, WsBc, and WsUInt8.

Referenced by main().

00312 {
00313     WsUInt8 i;
00314 
00315     for (i = 0; i < bc->num_function_names; i++)
00316         if (bc->function_names[i].index == index)
00317             return bc->function_names[i].name;
00318 
00319     return NULL;
00320 }

int main int  argc,
char *  argv[]
 

Definition at line 97 of file wmlsdasm.c.

References WsBcFunctionRec::code, WsBcFunctionRec::code_size, WsBcRec::constants, data, WsBcRec::function_names, WsBcRec::functions, getopt(), WsBcFunctionNameRec::index, lookup_function(), name, WsBcFunctionNameRec::name, WsBcRec::num_constants, WsBcRec::num_function_names, WsBcRec::num_functions, WsBcRec::num_pragmas, program, WsBcConstantRec::type, WsBcConstantRec::u, usage(), WsBcConstantRec::v_float, WsBcConstantRec::v_int, WsBcConstantRec::v_string, ws_asm_dasm(), WS_BC_CONST_TYPE_EMPTY_STRING, WS_BC_CONST_TYPE_FLOAT32, WS_BC_CONST_TYPE_FLOAT32_NAN, WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF, WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF, WS_BC_CONST_TYPE_INT, WS_BC_CONST_TYPE_UTF8_STRING, ws_bc_decode(), ws_bc_free(), ws_create(), ws_destroy(), ws_malloc(), ws_utf8_get_char(), WsBc, WsBool, WsCompilerPtr, WsUInt16, and WsUInt8.

00098 {
00099     int i;
00100     int opt;
00101     WsBool print_constants = WS_FALSE;
00102     WsBool print_function_names = WS_FALSE;
00103     WsBool print_functions = WS_FALSE;
00104     WsUInt8 k;
00105     WsUInt16 j;
00106 
00107     program = strrchr(argv[0], '/');
00108     if (program)
00109         program++;
00110     else
00111         program = argv[0];
00112 
00113     /* Process command line arguments. */
00114     while ((opt = getopt(argc, argv, "cfnh")) != EOF) {
00115         switch (opt) {
00116         case 'c':
00117             print_constants = WS_TRUE;
00118             break;
00119 
00120         case 'f':
00121             print_functions = WS_TRUE;
00122             break;
00123 
00124         case 'n':
00125             print_function_names = WS_TRUE;
00126             break;
00127 
00128         case 'h':
00129             usage();
00130             exit(0);
00131             break;
00132 
00133         case '?':
00134             printf("Try `%s -h' for a complete list of options.\n",
00135                    program);
00136             exit(1);
00137         }
00138     }
00139 
00140     for (i = optind; i < argc; i++) {
00141         FILE *fp;
00142         struct stat stat_st;
00143         unsigned char *data;
00144         size_t data_len;
00145         WsBc *bc;
00146 
00147         if (stat(argv[i], &stat_st) < 0) {
00148             fprintf(stderr, "%s: could not access `%s': %s\n",
00149                     program, argv[i], strerror(errno));
00150             exit(1);
00151         }
00152         data_len = stat_st.st_size;
00153 
00154         data = ws_malloc(data_len);
00155         if (data == NULL) {
00156             fprintf(stderr, "%s: out of memory: %s\n",
00157                     program, strerror(errno));
00158             exit(1);
00159         }
00160 
00161         fp = fopen(argv[i], "rb");
00162         if (fp == NULL) {
00163             fprintf(stderr, "%s: could not open input file `%s': %s\n",
00164                     program, argv[i], strerror(errno));
00165             exit(1);
00166         }
00167 
00168         if (fread(data, 1, data_len, fp) != data_len) {
00169             fprintf(stderr, "%s: could not read file `%s': %s\n",
00170                     program, argv[i], strerror(errno));
00171             exit(1);
00172         }
00173         fclose(fp);
00174 
00175         /* Decode byte-code. */
00176         bc = ws_bc_decode(data, data_len);
00177         if (bc == NULL) {
00178             fprintf(stderr, "%s: invalid byte-code file `%s'\n",
00179                     program, argv[i]);
00180             continue;
00181         }
00182 
00183         /* Print all requested data. */
00184         printf("\n%s:\t%lu bytes\n\n", argv[i], (unsigned long) data_len);
00185 
00186         if (print_constants) {
00187             printf("Disassembly of section Constants:\n\n");
00188             for (j = 0; j < bc->num_constants; j++) {
00189                 printf("%4d:\t", j);
00190                 switch (bc->constants[j].type) {
00191                 case WS_BC_CONST_TYPE_INT:
00192                     printf("%ld\n", (long) bc->constants[j].u.v_int);
00193                     break;
00194 
00195                 case WS_BC_CONST_TYPE_FLOAT32:
00196                     printf("%f\n", bc->constants[j].u.v_float);
00197                     break;
00198 
00199                 case WS_BC_CONST_TYPE_FLOAT32_NAN:
00200                     printf("NaN\n");
00201                     break;
00202 
00203                 case WS_BC_CONST_TYPE_FLOAT32_POSITIVE_INF:
00204                     printf("+infinity\n");
00205                     break;
00206 
00207                 case WS_BC_CONST_TYPE_FLOAT32_NEGATIVE_INF:
00208                     printf("-infinity\n");
00209                     break;
00210 
00211                 case WS_BC_CONST_TYPE_UTF8_STRING:
00212                     {
00213                         size_t pos = 0;
00214                         size_t column = 8;
00215                         unsigned long ch;
00216 
00217                         printf("\"");
00218 
00219                         while (ws_utf8_get_char(&bc->constants[j].u.v_string,
00220                                                 &ch, &pos)) {
00221                             if (ch < ' ') {
00222                                 printf("\\%02lx", ch);
00223                                 column += 3;
00224                             } else if (ch <= 0xff) {
00225                                 printf("%c", (unsigned char) ch);
00226                                 column++;
00227                             } else {
00228                                 printf("\\u%04lx", ch);
00229                                 column += 5;
00230                             }
00231                             if (column >= 75) {
00232                                 printf("\"\n\t\"");
00233                                 column = 8;
00234                             }
00235                         }
00236                         printf("\"\n");
00237                     }
00238                     break;
00239 
00240                 case WS_BC_CONST_TYPE_EMPTY_STRING:
00241                     printf("\"\"\n");
00242                     break;
00243                 }
00244             }
00245         }
00246 
00247         if (print_function_names) {
00248             printf("Disassembly of section Function names:\n\n");
00249             for (k = 0; k < bc->num_function_names; k++)
00250                 printf("  %-40.40s%8d\n",
00251                        bc->function_names[k].name,
00252                        bc->function_names[k].index);
00253         }
00254 
00255         if (print_functions) {
00256             WsCompilerPtr compiler = ws_create(NULL);
00257 
00258             printf("Disassembly of section Functions:\n");
00259 
00260             for (k = 0; k < bc->num_functions; k++) {
00261                 const char *name = lookup_function(bc, k);
00262 
00263                 printf("\nFunction %u", (unsigned int) k);
00264 
00265                 if (name)
00266                     printf(" <%s>", name);
00267 
00268                 printf(":\n");
00269 
00270                 ws_asm_dasm(compiler, bc->functions[k].code,
00271                             bc->functions[k].code_size);
00272             }
00273 
00274             ws_destroy(compiler);
00275         }
00276 
00277         if (!print_constants && !print_function_names && !print_functions) {
00278             printf("Sections:\n\
00279                    Name\t\t   Items\n\
00280                    Constants\t%8d\n\
00281                    Pragmas\t%8d\n\
00282                    Function names\t%8d\n\
00283                    Functions\t%8d\n",
00284                    bc->num_constants,
00285                    bc->num_pragmas,
00286                    bc->num_function_names,
00287                    bc->num_functions);
00288         }
00289 
00290         ws_bc_free(bc);
00291     }
00292 
00293     return 0;
00294 }

Here is the call graph for this function:

void usage void   )  [static]
 

Definition at line 298 of file wmlsdasm.c.

References program.

00299 {
00300     printf("Usage: %s OPTION... FILE...\n\
00301            \n\
00302            -c            print constants\n\
00303            -f            disassemble functions\n\
00304            -n            print function names\n\
00305            -h            print this help message and exit successfully\n\
00306            \n",
00307            program);
00308 }


Variable Documentation

char* program [static]
 

Definition at line 93 of file wmlsdasm.c.

Referenced by main(), and usage().

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.