Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Correct various things to pass codingstd_tests

Fix trailing whitespace, whitespace near parens, cuddled elses and line
lengths.
  • Loading branch information...
commit 8eeaa67a18a2ae76b5f32a5529cc37886fab8e1e 1 parent c201ff9
@nbrown nbrown authored
View
2  config/gen/makefiles/root.in
@@ -2329,7 +2329,7 @@ m0_c_tests :
#IF(win32): set M0_INTERP=.\src\m0\c\m0$(EXE) && $(PERL) t/harness $(M0_TEST_FILES)
#UNLESS(win32): M0_INTERP=./src/m0/c/m0$(EXE) $(PERL) t/harness $(M0_TEST_FILES)
-m0_c_debugger_tests :
+m0_c_debugger_tests :
#IF(win32): set M0_INTERP=.\src\m0\c\m0-debugger$(EXE) && $(PERL) t/harness $(M0_DEBUGGER_TEST_FILES)
#UNLESS(win32): M0_INTERP=./src/m0/c/m0-debugger$(EXE) $(PERL) t/harness $(M0_DEBUGGER_TEST_FILES)
View
2  src/m0/c/include/m0_mob.h
@@ -3,7 +3,7 @@
# include "m0_mob_structures.h"
# include "m0_interp_structures.h"
-int load_mob_file( M0_Interp *interp, const char *filename );
+int load_mob_file(M0_Interp *interp, const char *filename);
# define M0_MOB_H 1
#endif
View
6 src/m0/c/include/m0_ops.h
@@ -47,10 +47,10 @@ enum M0_OPS {
M0_PRINT_N,
M0_EXIT
/* end_gen */
-};
-
+};
+
int
-run_ops( M0_Interp *interp, M0_CallFrame *cf );
+run_ops(M0_Interp *interp, M0_CallFrame *cf);
# define M0_OPS_H 1
#endif
View
118 src/m0/c/m0_debugger.c
@@ -327,29 +327,29 @@ M0_Debugger_Command
str_to_db_cmd(char* str) {
M0_Debugger_Command cmd = Invalid;
- if(strcmp(str,"c") == 0)
+ if (strcmp(str, "c") == 0)
cmd = Continue;
- else if(strcmp(str, "s") == 0)
+ else if (strcmp(str, "s") == 0)
cmd = Step;
- else if(strcmp(str, "p") == 0)
+ else if (strcmp(str, "p") == 0)
cmd = Print;
- else if(strcmp(str, "pi") == 0)
+ else if (strcmp(str, "pi") == 0)
cmd = Print_Integer;
- else if(strcmp(str, "pn") == 0)
+ else if (strcmp(str, "pn") == 0)
cmd = Print_Number;
- else if(strcmp(str, "ps") == 0)
+ else if (strcmp(str, "ps") == 0)
cmd = Print_String;
- else if(strcmp(str, "l") == 0)
+ else if (strcmp(str, "l") == 0)
cmd = List;
- else if(strcmp(str, "b") == 0)
+ else if (strcmp(str, "b") == 0)
cmd = Add_Breakpoint;
- else if(strcmp(str, "B") == 0)
+ else if (strcmp(str, "B") == 0)
cmd = Delete_Breakpoint;
- else if(strcmp(str, "L") == 0)
+ else if (strcmp(str, "L") == 0)
cmd = List_Breakpoints;
- else if(strcmp(str, "h") == 0)
+ else if (strcmp(str, "h") == 0)
cmd = Help;
- else if(strcmp(str, "") == 0)
+ else if (strcmp(str, "") == 0)
cmd = None;
return cmd;
}
@@ -357,12 +357,12 @@ str_to_db_cmd(char* str) {
char * get_line(char* str, size_t size, FILE *f) {
char *p = fgets(str, size, f);
- if(p) {
- size_t last = strlen (str) - 1;
+ if (p) {
+ size_t last = strlen(str) - 1;
if (str[last] == '\n')
str[last] = '\0';
}
- if(0 == strlen(str)) {
+ if (0 == strlen(str)) {
free(p);
p = NULL;
}
@@ -374,16 +374,16 @@ get_script_file_input(char *filename, char *input) {
char *p;
static FILE *pFile = NULL;
- if(pFile == NULL) {
+ if (pFile == NULL) {
pFile = fopen(filename, "r");
}
- if(pFile == NULL)
+ if (pFile == NULL)
perror("Error opening file");
p = get_line(input, 100, pFile);
- if(feof(pFile)) {
+ if (feof(pFile)) {
fclose(pFile);
return NULL;
}
@@ -394,7 +394,7 @@ char *
get_db_input(M0_Debugger_Info *db_info, M0_Debugger_Command *cmd, char *input) {
char *p;
- if(db_info->input_source)
+ if (db_info->input_source)
p = get_script_file_input(db_info->input_source, input);
else
p = get_line(input, 100, stdin);
@@ -403,21 +403,21 @@ get_db_input(M0_Debugger_Info *db_info, M0_Debugger_Command *cmd, char *input) {
char *arg = NULL;
char *tok = NULL;
- if(db_info->input_source != NULL)
+ if (db_info->input_source != NULL)
printf("%s\n", input);
tok = strtok(input, " ");
- if(tok)
+ if (tok)
*cmd = str_to_db_cmd(tok);
else
return input;
tok = strtok(NULL, " ");
- if(tok) {
- arg = (char*)realloc(arg, (strlen(tok)+1) * sizeof(char));
- if(!arg)
+ if (tok) {
+ arg = (char*)realloc(arg, (strlen(tok)+1) * sizeof (char));
+ if (!arg)
perror("Could not allocate memory for arg");
- strcpy(arg,tok);
+ strcpy(arg, tok);
}
return arg;
}
@@ -439,7 +439,7 @@ char * register_to_name(const unsigned char reg) {
unsigned int name_to_register_id(const char *name) {
unsigned int reg = -1;
unsigned int i = 0;
- for ( ; i < 256; i++) {
+ for (; i < 256; i++) {
if (strcmp(name, M0_REGISTER_NAMES[i]) == 0) {
reg = i;
break;
@@ -453,15 +453,15 @@ debug_print(M0_Debugger_Command cmd, M0_CallFrame *cf, char *arg)
{
int reg = name_to_register_id(arg);
unsigned long *a, *b;
- switch(cmd) {
+ switch (cmd) {
case Print_String:
printf("%s\n", (char *)(cf->registers[ reg ] + 8));
break;
case Print_Integer:
- printf("%d\n", (unsigned int)(cf->registers[ reg ] ));
+ printf("%d\n", (unsigned int)(cf->registers[ reg ]));
break;
case Print_Number:
- printf("%f\n", (float)(cf->registers[ reg ] ));
+ printf("%f\n", (float)(cf->registers[ reg ]));
break;
case Print:
default:
@@ -484,11 +484,11 @@ debug_list(M0_CallFrame *cf, const unsigned char *ops, const unsigned long pc)
case M0_GOTO_IF:
case M0_GOTO:
case M0_GOTO_CHUNK:
- arg1 = calloc(10,sizeof(char));
+ arg1 = calloc(10, sizeof (char));
sprintf(arg1, "%d", (int)(ops[pc*4+1]));
case M0_SET_IMM:
- arg2 = calloc(10,sizeof(char));
- arg3 = calloc(10,sizeof(char));
+ arg2 = calloc(10, sizeof (char));
+ arg3 = calloc(10, sizeof (char));
sprintf(arg2, "%d", (int)(ops[pc*4+2]));
sprintf(arg3, "%d", (int)(ops[pc*4+3]));
break;
@@ -504,13 +504,13 @@ debug_add_breakpoint(char * arg, M0_Debugger_Info* db_info)
{
unsigned long bp;
unsigned int n_bp;
- if(!arg) {
+ if (!arg) {
printf("You must specify a PC in order to add a breakpoint\n");
return;
}
bp = strtoul(arg, NULL, 10);
n_bp = ++(db_info->n_breakpoints);
- db_info->breakpoints = realloc(db_info->breakpoints, n_bp*sizeof(unsigned long));
+ db_info->breakpoints = realloc(db_info->breakpoints, n_bp*sizeof (unsigned long));
db_info->breakpoints[n_bp-1] = bp;
}
@@ -519,18 +519,19 @@ debug_delete_breakpoint(char * arg, M0_Debugger_Info* db_info)
{
unsigned long bp;
unsigned long n_bp;
- if(!arg) {
+ if (!arg) {
bp = 0;
db_info->n_breakpoints = n_bp = 0;
- db_info->breakpoints = realloc(db_info->breakpoints, n_bp*sizeof(unsigned long));
- } else {
+ db_info->breakpoints = realloc(db_info->breakpoints, n_bp*sizeof (unsigned long));
+ } else
+ {
n_bp = --(db_info->n_breakpoints);
bp = strtoul(arg, NULL, 10);
- if(bp < db_info->n_breakpoints) {
- for( ; bp < db_info->n_breakpoints; bp++)
+ if (bp < db_info->n_breakpoints) {
+ for (; bp < db_info->n_breakpoints; bp++)
db_info->breakpoints[bp] = db_info->breakpoints[bp+1];
}
- db_info->breakpoints = realloc(db_info->breakpoints, n_bp*sizeof(unsigned long));
+ db_info->breakpoints = realloc(db_info->breakpoints, n_bp*sizeof (unsigned long));
}
}
@@ -542,7 +543,7 @@ debug_list_breakpoints(char * arg, M0_Debugger_Info* db_info)
UNUSED(arg);
printf("There are %d breakpoint(s)\n", n_bp);
- for( ; i < n_bp; i++)
+ for (; i < n_bp; i++)
printf("Breakpoint #%u:\tPC=%lu\n", i, db_info->breakpoints[i]);
}
@@ -553,9 +554,12 @@ print_help()
printf("\tc : continue until the next breakpoint or the end of the program\n");
printf("\ts : single step (execute the next m0 command)\n");
printf("\tp ARG: print ARG (treat ARG as hex)\n\t\tCurrently ARG only supports registers\n");
- printf("\tpi ARG: print ARG (treat ARG as an unsigned integer)\n\t\tCurrently ARG only supports registers\n");
- printf("\tpn ARG: print ARG (treat ARG as a float)\n\t\tCurrently ARG only supports registers\n");
- printf("\tps ARG: print ARG (treat ARG as a string)\n\t\tCurrently ARG only supports registers\n");
+ printf("\tpi ARG: print ARG (treat ARG as an unsigned integer)\n");
+ printf("\t\tCurrently ARG only supports registers\n");
+ printf("\tpn ARG: print ARG (treat ARG as a float)\n");
+ printf("\t\tCurrently ARG only supports registers\n");
+ printf("\tps ARG: print ARG (treat ARG as a string)\n");
+ printf("\t\tCurrently ARG only supports registers\n");
printf("\tl : list the decompiled source code for the line that is about to be executed\n");
printf("\tb PC : create a new breakpoint at PC\n");
printf("\tB ARG : delete the breakpoint # ARG\n");
@@ -565,19 +569,20 @@ print_help()
}
static void
-db_prompt(M0_Debugger_Info *db_info, M0_CallFrame *cf, const unsigned char *ops, const unsigned long pc)
+db_prompt(M0_Debugger_Info *db_info, M0_CallFrame *cf, const unsigned char *ops,
+ const unsigned long pc)
{
static M0_Debugger_Command last_cmd = None;
static char *last_arg = NULL;
int done = 0;
- char *user_input = calloc(100, sizeof(char));
+ char *user_input = calloc(100, sizeof (char));
- while(!done) {
+ while (!done) {
M0_Debugger_Command cmd = None;
char *arg = NULL;
printf("PC=%lu> ", pc);
arg = get_db_input(db_info, &cmd, user_input);
- if(cmd == None) {
+ if (cmd == None) {
cmd = last_cmd;
arg = last_arg;
}
@@ -587,7 +592,7 @@ db_prompt(M0_Debugger_Info *db_info, M0_CallFrame *cf, const unsigned char *ops,
}
switch (cmd) {
case Continue:
- if(db_info->n_breakpoints > 0)
+ if (db_info->n_breakpoints > 0)
db_info->state = BREAK;
else
db_info->state = RUN;
@@ -632,8 +637,8 @@ int
check_breakpoints(M0_Debugger_Info *db_info, const unsigned long pc) {
unsigned int i = 0;
unsigned int bp_found = 0;
- for(; i < db_info->n_breakpoints; i++)
- if(db_info->breakpoints[i] == pc) {
+ for (; i < db_info->n_breakpoints; i++)
+ if (db_info->breakpoints[i] == pc) {
bp_found = 1;
break;
}
@@ -643,12 +648,12 @@ check_breakpoints(M0_Debugger_Info *db_info, const unsigned long pc) {
static void
parse_argv(M0_Debugger_Info *db_info, int argc, const char* argv[])
{
- if(argc > 2) {
+ if (argc > 2) {
int i = 2;
- while(i < argc) {
- if(strcmp("-s",argv[i]) == 0) {
+ while (i < argc) {
+ if (strcmp("-s", argv[i]) == 0) {
size_t size = strlen(argv[++i]);
- db_info->input_source = malloc((size+1)*sizeof(char));
+ db_info->input_source = malloc((size+1)*sizeof (char));
strcpy(db_info->input_source, argv[i]);
}
i++;
@@ -658,7 +663,8 @@ parse_argv(M0_Debugger_Info *db_info, int argc, const char* argv[])
}
void
-debugger(int argc, const char* argv[], M0_Interp *interp, M0_CallFrame *cf, const unsigned char *ops, const unsigned long pc)
+debugger(int argc, const char* argv[], M0_Interp *interp, M0_CallFrame *cf,
+ const unsigned char *ops, const unsigned long pc)
{
static M0_Debugger_Info db_info;
UNUSED(interp);
View
115 src/m0/c/m0_interp.c
@@ -12,28 +12,28 @@ static M0_Interp *
new_interp();
static M0_CallFrame *
-new_call_frame( M0_Interp *interp );
+new_call_frame(M0_Interp *interp);
static void
-call_frame_free( M0_Interp *interp, M0_CallFrame *cf );
+call_frame_free(M0_Interp *interp, M0_CallFrame *cf);
static void
-interp_free( M0_Interp *interp );
+interp_free(M0_Interp *interp);
static void
-m0_chunk_free( M0_Chunk *chunk );
+m0_chunk_free(M0_Chunk *chunk);
void
-m0_chunk_free_constants( M0_Constants_Segment *constants );
+m0_chunk_free_constants(M0_Constants_Segment *constants);
void
-m0_chunk_free_metadata( M0_Metadata_Segment *metadata );
+m0_chunk_free_metadata(M0_Metadata_Segment *metadata);
void
-m0_chunk_free_bytecode( M0_Bytecode_Segment *bytecode );
+m0_chunk_free_bytecode(M0_Bytecode_Segment *bytecode);
int
-main( int argc, const char *argv[]) {
+main(int argc, const char *argv[]) {
M0_Interp *interp = new_interp();
int i;
uint64_t* interp_argv;
@@ -45,60 +45,61 @@ main( int argc, const char *argv[]) {
debugger(argc, argv, interp, NULL, NULL, -1);
#endif
(*interp)[ARGC] = argc - 1;
- interp_argv = (uint64_t*) malloc((argc-1) * sizeof(uint64_t));
+ interp_argv = (uint64_t*) malloc((argc-1) * sizeof (uint64_t));
- // encode cli arguments as M0 strings, skipping the first (name of the interp)
+ /* encode cli arguments as M0 strings, skipping the first (name of the interp) */
for (i = 1; i < argc; i++) {
const char *arg = argv[i];
- uint32_t m0_arg_const_len = strlen(arg) + 4 + 4 + 1; // byte count + encoding + string bytes + terminal null
+ /* byte count + encoding + string bytes + terminal null */
+ uint32_t m0_arg_const_len = strlen(arg) + 4 + 4 + 1;
uint32_t m0_arg_len = strlen(arg) + 1;
uint32_t m0_arg_encoding = 0;
- char *m0_arg_const = (char*) malloc(m0_arg_const_len * sizeof(char));
- memcpy(&(m0_arg_const[0]), &m0_arg_len, sizeof(uint32_t));
- memcpy(&(m0_arg_const[4]), &m0_arg_encoding, sizeof(uint32_t));
- memcpy(&(m0_arg_const[8]), arg, m0_arg_len * sizeof(char));
+ char *m0_arg_const = (char*) malloc(m0_arg_const_len * sizeof (char));
+ memcpy(&(m0_arg_const[0]), &m0_arg_len, sizeof (uint32_t));
+ memcpy(&(m0_arg_const[4]), &m0_arg_encoding, sizeof (uint32_t));
+ memcpy(&(m0_arg_const[8]), arg, m0_arg_len * sizeof (char));
interp_argv[i-1] = m0_arg_const;
}
(*interp)[ARGV] = interp_argv;
if (argc < 2) {
- fprintf( stderr, "Usage: m0 <filename.mob>\n" );
- interp_free( interp );
+ fprintf(stderr, "Usage: m0 <filename.mob>\n");
+ interp_free(interp);
exit(1);
}
- if (!load_mob_file( interp, argv[1] )) {
- fprintf( stderr, "Could not load m0b file: '%s'\n", argv[1] );
- interp_free( interp );
- exit( 1 );
+ if (!load_mob_file(interp, argv[1])) {
+ fprintf(stderr, "Could not load m0b file: '%s'\n", argv[1]);
+ interp_free(interp);
+ exit(1);
}
else {
- M0_CallFrame *cf = new_call_frame( interp );
- run_ops( interp, cf );
+ M0_CallFrame *cf = new_call_frame(interp);
+ run_ops(interp, cf);
- call_frame_free( interp, cf );
- interp_free( interp );
+ call_frame_free(interp, cf);
+ interp_free(interp);
- exit( 0 );
+ exit(0);
}
}
M0_Interp *
new_interp() {
- M0_Interp *interp = calloc( 1, sizeof (M0_Interp) );
- M0_Config *config = calloc( 1, sizeof (M0_Config) );
+ M0_Interp *interp = calloc(1, sizeof (M0_Interp));
+ M0_Config *config = calloc(1, sizeof (M0_Config));
(*config)[CFG_REGSZ] = 8;
- (*config)[CFG_CFSZ] = sizeof( M0_CallFrame );
+ (*config)[CFG_CFSZ] = sizeof (M0_CallFrame);
(*interp)[CONFIG] = (uint64_t)config;
return interp;
}
M0_CallFrame *
-new_call_frame( M0_Interp *interp ) {
- M0_CallFrame *frame = malloc( sizeof (M0_CallFrame) );
+new_call_frame(M0_Interp *interp) {
+ M0_CallFrame *frame = malloc(sizeof (M0_CallFrame));
unsigned int i;
- for(i = 0; i < 256; i++)
+ for (i = 0; i < 256; i++)
frame->registers[i] = (uint64_t)0;
/* this is a silly minimal hack for now */
@@ -112,76 +113,76 @@ new_call_frame( M0_Interp *interp ) {
}
void
-call_frame_free( M0_Interp *interp, M0_CallFrame *cf ) {
+call_frame_free(M0_Interp *interp, M0_CallFrame *cf) {
UNUSED(interp);
- free( cf );
+ free(cf);
}
void
-interp_free( M0_Interp *interp ) {
+interp_free(M0_Interp *interp) {
M0_Chunk *chunk = (M0_Chunk*)((*interp)[CHUNKS]);
while (chunk) {
M0_Chunk *next = chunk->next;
- m0_chunk_free( chunk );
+ m0_chunk_free(chunk);
chunk = next;
}
- free( ((void *)(*interp)[CONFIG]) );
- free( interp );
+ free(((void *)(*interp)[CONFIG]));
+ free(interp);
}
void
-m0_chunk_free( M0_Chunk *chunk ) {
+m0_chunk_free(M0_Chunk *chunk) {
if (chunk->constants)
- m0_chunk_free_constants( chunk->constants );
+ m0_chunk_free_constants(chunk->constants);
if (chunk->metadata)
- m0_chunk_free_metadata( chunk->metadata );
+ m0_chunk_free_metadata(chunk->metadata);
if (chunk->bytecode)
- m0_chunk_free_bytecode( chunk->bytecode );
+ m0_chunk_free_bytecode(chunk->bytecode);
- free( (char *)chunk->name );
- free( chunk );
+ free((char *)chunk->name);
+ free(chunk);
}
void
-m0_chunk_free_constants( M0_Constants_Segment *constants )
+m0_chunk_free_constants(M0_Constants_Segment *constants)
{
const unsigned long count = constants->count;
unsigned long i = 0;
for (i = 0; i < count; i++) {
- if ( constants->consts[i] && constants->pointers[i])
- free( (void *)constants->consts[i] );
+ if (constants->consts[i] && constants->pointers[i])
+ free((void *)constants->consts[i]);
}
- free( constants->consts );
- free( constants );
+ free(constants->consts);
+ free(constants);
}
void
-m0_chunk_free_metadata( M0_Metadata_Segment *metadata )
+m0_chunk_free_metadata(M0_Metadata_Segment *metadata)
{
const unsigned long count = metadata->count;
unsigned long i = 0;
for (i = 0; i < count; i++) {
- if ( metadata->entries[i] )
- free( (void *)metadata->entries[i] );
+ if (metadata->entries[i])
+ free((void *)metadata->entries[i]);
}
- free( metadata->entries );
- free( metadata );
+ free(metadata->entries);
+ free(metadata);
}
void
-m0_chunk_free_bytecode( M0_Bytecode_Segment *bytecode )
+m0_chunk_free_bytecode(M0_Bytecode_Segment *bytecode)
{
if (bytecode->ops)
- free( bytecode->ops );
+ free(bytecode->ops);
- free( bytecode );
+ free(bytecode);
}
/*
View
231 src/m0/c/m0_mob.c
@@ -8,155 +8,156 @@
#include "include/m0_compiler_defines.h"
static int
-parse_mob_header( M0_Interp *interp, FILE *stream );
+parse_mob_header(M0_Interp *interp, FILE *stream);
static int
-parse_header_config( M0_Interp *interp, FILE *stream );
+parse_header_config(M0_Interp *interp, FILE *stream);
static int
-parse_mob_dirseg( M0_Interp *interp, FILE *stream );
+parse_mob_dirseg(M0_Interp *interp, FILE *stream);
static int
-parse_mob_chunks( M0_Interp *interp, FILE *stream );
+parse_mob_chunks(M0_Interp *interp, FILE *stream);
static M0_Chunk *
-add_chunk( M0_Interp *interp, const char *name,
+add_chunk(M0_Interp *interp, const char *name,
unsigned long chunk_id, unsigned long name_length,
M0_Chunk *chunk);
static M0_Constants_Segment *
-parse_mob_constants_segment( M0_Interp *interp, FILE *stream );
+parse_mob_constants_segment(M0_Interp *interp, FILE *stream);
static M0_Metadata_Segment *
-parse_mob_metadata_segment( M0_Interp *interp, FILE *stream );
+parse_mob_metadata_segment(M0_Interp *interp, FILE *stream);
static M0_Bytecode_Segment *
-parse_mob_bytecode_segment( M0_Interp *interp, FILE *stream );
+parse_mob_bytecode_segment(M0_Interp *interp, FILE *stream);
static void *
-read_from_stream( FILE *stream, size_t bytes );
+read_from_stream(FILE *stream, size_t bytes);
static unsigned int
-read_int_from_stream( FILE *stream );
+read_int_from_stream(FILE *stream);
static unsigned long
-read_long_from_stream( FILE *stream );
+read_long_from_stream(FILE *stream);
static unsigned int
-read_padding_from_stream( FILE *stream, size_t bytes );
+read_padding_from_stream(FILE *stream, size_t bytes);
static int
-verify_mob_magic_number( M0_Interp *interp, FILE *stream );
+verify_mob_magic_number(M0_Interp *interp, FILE *stream);
static int
-validate_mob_version( M0_Interp *interp, FILE *stream );
+validate_mob_version(M0_Interp *interp, FILE *stream);
static int
-validate_segment_identifier( M0_Interp *interp, FILE *stream,
- unsigned int seg_id );
+validate_segment_identifier(M0_Interp *interp, FILE *stream,
+ unsigned int seg_id);
int
load_mob_file(M0_Interp *interp, const char *filename) {
- FILE *mob = fopen( filename, "rb" );
+ FILE *mob = fopen(filename, "rb");
if (!mob)
return 0;
- if (!parse_mob_header( interp, mob )) {
- fclose( mob );
+ if (!parse_mob_header(interp, mob)) {
+ fclose(mob);
return 0;
}
- if (!parse_mob_dirseg( interp, mob )) {
- fclose( mob );
+ if (!parse_mob_dirseg(interp, mob)) {
+ fclose(mob);
return 0;
}
- if (!parse_mob_chunks( interp, mob ))
+ if (!parse_mob_chunks(interp, mob))
{
- fclose( mob );
+ fclose(mob);
return 0;
}
- return fclose( mob ) == 0;
+ return fclose(mob) == 0;
}
int
-parse_mob_header( M0_Interp *interp, FILE *stream ) {
- if (!verify_mob_magic_number( interp, stream ))
+parse_mob_header(M0_Interp *interp, FILE *stream) {
+ if (!verify_mob_magic_number(interp, stream))
return 0;
- if (!validate_mob_version( interp, stream ))
+ if (!validate_mob_version(interp, stream))
return 0;
- if (!parse_header_config( interp, stream ))
+ if (!parse_header_config(interp, stream))
return 0;
- return read_padding_from_stream( stream, 2 );
+ return read_padding_from_stream(stream, 2);
}
int
-verify_mob_magic_number( M0_Interp *interp, FILE *stream ) {
- char *magic = (char *)read_from_stream( stream, 8 );
+verify_mob_magic_number(M0_Interp *interp, FILE *stream) {
+ char *magic = (char *)read_from_stream(stream, 8);
const char header[8] = { 254, 77, 48, 66, 13, 10, 26, 10 };
UNUSED(interp);
- if (strncmp( magic, header, 8 ) != 0) {
- fprintf( stderr, "Invalid M0B header\n" );
- free( magic );
+ if (strncmp(magic, header, 8) != 0) {
+ fprintf(stderr, "Invalid M0B header\n");
+ free(magic);
return 0;
}
- free( magic );
+ free(magic);
return 1;
}
int
-validate_mob_version( M0_Interp *interp, FILE *stream ) {
- int version = read_int_from_stream( stream );
+validate_mob_version(M0_Interp *interp, FILE *stream) {
+ int version = read_int_from_stream(stream);
if (version == 0) {
((uint64_t*)(*interp)[CONFIG])[CFG_M0V] = (uint64_t)0;
return 1;
}
- fprintf( stderr, "Can't read m0b version %d\n", version );
+ fprintf(stderr, "Can't read m0b version %d\n", version);
return 0;
}
int
-parse_header_config( M0_Interp *interp, FILE *stream ) {
- ((uint64_t*)(*interp)[CONFIG])[CFG_IREGSZ] = read_int_from_stream( stream );
- ((uint64_t*)(*interp)[CONFIG])[CFG_NREGSZ] = read_int_from_stream( stream );
- ((uint64_t*)(*interp)[CONFIG])[CFG_OPCODESZ] = read_int_from_stream( stream );
- ((uint64_t*)(*interp)[CONFIG])[CFG_PTRSZ] = read_int_from_stream( stream );
- ((uint64_t*)(*interp)[CONFIG])[CFG_ENDIANNESS] = read_int_from_stream( stream );
+parse_header_config(M0_Interp *interp, FILE *stream) {
+ ((uint64_t*)(*interp)[CONFIG])[CFG_IREGSZ] = read_int_from_stream(stream);
+ ((uint64_t*)(*interp)[CONFIG])[CFG_NREGSZ] = read_int_from_stream(stream);
+ ((uint64_t*)(*interp)[CONFIG])[CFG_OPCODESZ] = read_int_from_stream(stream);
+ ((uint64_t*)(*interp)[CONFIG])[CFG_PTRSZ] = read_int_from_stream(stream);
+ ((uint64_t*)(*interp)[CONFIG])[CFG_ENDIANNESS] = read_int_from_stream(stream);
return 1;
}
int
-parse_mob_dirseg( M0_Interp *interp, FILE *stream ) {
- if (!validate_segment_identifier( interp, stream, M0_DIR_SEG ))
+parse_mob_dirseg(M0_Interp *interp, FILE *stream) {
+ if (!validate_segment_identifier(interp, stream, M0_DIR_SEG))
return 0;
- else {
- const unsigned long seg_entry_count = read_long_from_stream( stream );
- const unsigned long seg_byte_count = read_long_from_stream( stream );
+ else
+ {
+ const unsigned long seg_entry_count = read_long_from_stream(stream);
+ const unsigned long seg_byte_count = read_long_from_stream(stream);
unsigned long chunks_found = 0;
M0_Chunk *last_chunk = NULL;
UNUSED(seg_byte_count);
for (; chunks_found < seg_entry_count; chunks_found++) {
- const unsigned long seg_offset = read_long_from_stream( stream );
- const unsigned long name_length = read_long_from_stream( stream );
+ const unsigned long seg_offset = read_long_from_stream(stream);
+ const unsigned long name_length = read_long_from_stream(stream);
const char *name =
- read_from_stream( stream, name_length );
+ read_from_stream(stream, name_length);
UNUSED(seg_offset);
- last_chunk = add_chunk( interp, name, chunks_found, name_length, last_chunk);
+ last_chunk = add_chunk(interp, name, chunks_found, name_length, last_chunk);
}
}
@@ -165,13 +166,14 @@ parse_mob_dirseg( M0_Interp *interp, FILE *stream ) {
}
M0_Chunk *
-add_chunk( M0_Interp *interp, const char *name, unsigned long chunk_id,
+add_chunk(M0_Interp *interp, const char *name, unsigned long chunk_id,
unsigned long name_length,
M0_Chunk *chunk) {
if (!chunk) {
- chunk = malloc( sizeof (M0_Chunk) );
- } else {
- chunk->next = malloc( sizeof (M0_Chunk) );
+ chunk = malloc(sizeof (M0_Chunk));
+ } else
+ {
+ chunk->next = malloc(sizeof (M0_Chunk));
chunk = chunk->next;
}
chunk->id = chunk_id;
@@ -186,20 +188,20 @@ add_chunk( M0_Interp *interp, const char *name, unsigned long chunk_id,
}
int
-validate_segment_identifier( M0_Interp *interp, FILE *stream,
- unsigned int seg_id ) {
+validate_segment_identifier(M0_Interp *interp, FILE *stream,
+ unsigned int seg_id) {
UNUSED(interp);
- return read_long_from_stream( stream ) == seg_id;
+ return read_long_from_stream(stream) == seg_id;
}
int
-parse_mob_chunks( M0_Interp *interp, FILE *stream ) {
+parse_mob_chunks(M0_Interp *interp, FILE *stream) {
M0_Chunk *chunk = (M0_Chunk*)(*interp)[CHUNKS];
while (chunk) {
- chunk->constants = parse_mob_constants_segment( interp, stream );
- chunk->metadata = parse_mob_metadata_segment( interp, stream );
- chunk->bytecode = parse_mob_bytecode_segment( interp, stream );
+ chunk->constants = parse_mob_constants_segment(interp, stream);
+ chunk->metadata = parse_mob_metadata_segment(interp, stream);
+ chunk->bytecode = parse_mob_bytecode_segment(interp, stream);
chunk = chunk->next;
}
@@ -208,24 +210,25 @@ parse_mob_chunks( M0_Interp *interp, FILE *stream ) {
}
M0_Constants_Segment *
-parse_mob_constants_segment( M0_Interp *interp, FILE *stream ) {
- if (!validate_segment_identifier( interp, stream, M0_CONST_SEG ))
+parse_mob_constants_segment(M0_Interp *interp, FILE *stream) {
+ if (!validate_segment_identifier(interp, stream, M0_CONST_SEG))
return 0;
- else {
- const unsigned long const_count = read_long_from_stream( stream );
- const unsigned long byte_count = read_long_from_stream( stream );
+ else
+ {
+ const unsigned long const_count = read_long_from_stream(stream);
+ const unsigned long byte_count = read_long_from_stream(stream);
unsigned long i = 0;
M0_Constants_Segment *segment =
- malloc( sizeof( M0_Constants_Segment ) );
- segment->consts = calloc(const_count, sizeof ( uint64_t ));
- segment->pointers = calloc(const_count, sizeof ( unsigned int));
+ malloc(sizeof (M0_Constants_Segment));
+ segment->consts = calloc(const_count, sizeof (uint64_t));
+ segment->pointers = calloc(const_count, sizeof (unsigned int));
segment->count = const_count;
- UNUSED( byte_count );
+ UNUSED(byte_count);
for (i = 0; i < const_count; i++) {
- const unsigned long length = read_long_from_stream( stream );
- const char *constant = (char *) read_from_stream( stream, length);
+ const unsigned long length = read_long_from_stream(stream);
+ const char *constant = (char *) read_from_stream(stream, length);
if (length > 8) {
int encoding = *(long *)&(constant[4]);
if (encoding == 0) {
@@ -235,7 +238,8 @@ parse_mob_constants_segment( M0_Interp *interp, FILE *stream ) {
segment->consts[i] = (uint64_t)constant;
segment->pointers[i] = 1;
}
- } else {
+ } else
+ {
if (constant)
segment->consts[i] = *(uint64_t*)constant;
}
@@ -249,27 +253,28 @@ parse_mob_constants_segment( M0_Interp *interp, FILE *stream ) {
}
M0_Metadata_Segment *
-parse_mob_metadata_segment( M0_Interp *interp, FILE *stream ) {
- if (!validate_segment_identifier( interp, stream, M0_META_SEG ))
+parse_mob_metadata_segment(M0_Interp *interp, FILE *stream) {
+ if (!validate_segment_identifier(interp, stream, M0_META_SEG))
return 0;
- else {
- const unsigned long entry_count = read_long_from_stream( stream );
- const unsigned long byte_count = read_long_from_stream( stream );
+ else
+ {
+ const unsigned long entry_count = read_long_from_stream(stream);
+ const unsigned long byte_count = read_long_from_stream(stream);
unsigned long i = 0;
M0_Metadata_Segment *segment =
- malloc( sizeof( M0_Metadata_Segment) );
+ malloc(sizeof (M0_Metadata_Segment));
segment->entries =
- malloc( sizeof( M0_Metadata_Entry * ) * entry_count );
+ malloc(sizeof (M0_Metadata_Entry *) * entry_count);
segment->count = entry_count;
- UNUSED( byte_count );
+ UNUSED(byte_count);
for (i = 0; i < entry_count; i++) {
- M0_Metadata_Entry *entry = malloc( sizeof( M0_Metadata_Entry ) );
+ M0_Metadata_Entry *entry = malloc(sizeof (M0_Metadata_Entry));
- entry->offset = read_long_from_stream( stream );
- entry->name_index = read_long_from_stream( stream );
- entry->value_index = read_long_from_stream( stream );
+ entry->offset = read_long_from_stream(stream);
+ entry->name_index = read_long_from_stream(stream);
+ entry->value_index = read_long_from_stream(stream);
segment->entries[i] = entry;
}
@@ -281,20 +286,21 @@ parse_mob_metadata_segment( M0_Interp *interp, FILE *stream ) {
}
M0_Bytecode_Segment *
-parse_mob_bytecode_segment( M0_Interp *interp, FILE *stream ) {
- if (!validate_segment_identifier( interp, stream, M0_BC_SEG ))
+parse_mob_bytecode_segment(M0_Interp *interp, FILE *stream) {
+ if (!validate_segment_identifier(interp, stream, M0_BC_SEG))
return 0;
- else {
- M0_Bytecode_Segment *bytecode = malloc( sizeof (M0_Bytecode_Segment) );
- const unsigned long op_count = read_long_from_stream( stream );
- const unsigned long num_bytes = read_long_from_stream( stream );
+ else
+ {
+ M0_Bytecode_Segment *bytecode = malloc(sizeof (M0_Bytecode_Segment));
+ const unsigned long op_count = read_long_from_stream(stream);
+ const unsigned long num_bytes = read_long_from_stream(stream);
UNUSED(num_bytes);
/* XXX: num_bytes can be zero. Sigh. */
bytecode->op_count = op_count;
bytecode->ops = (unsigned char *)
- read_from_stream( stream, op_count * 4 );
+ read_from_stream(stream, op_count * 4);
return bytecode;
}
@@ -303,14 +309,15 @@ parse_mob_bytecode_segment( M0_Interp *interp, FILE *stream ) {
}
void *
-read_from_stream( FILE *stream, size_t bytes ) {
- if (feof( stream ))
+read_from_stream(FILE *stream, size_t bytes) {
+ if (feof(stream))
return NULL;
- else {
- void *data = malloc( bytes );
+ else
+ {
+ void *data = malloc(bytes);
if (fread(data, 1, bytes, stream) != bytes) {
- free( data );
+ free(data);
return NULL;
}
@@ -319,13 +326,14 @@ read_from_stream( FILE *stream, size_t bytes ) {
}
unsigned int
-read_int_from_stream( FILE *stream ) {
- if (feof( stream ))
+read_int_from_stream(FILE *stream) {
+ if (feof(stream))
return 0;
- else {
+ else
+ {
int value = 0;
- if (fread( &value, 1, 1, stream) != 1)
+ if (fread(&value, 1, 1, stream) != 1)
return 0;
return value;
@@ -333,13 +341,14 @@ read_int_from_stream( FILE *stream ) {
}
unsigned long
-read_long_from_stream( FILE *stream ) {
- if (feof( stream ))
+read_long_from_stream(FILE *stream) {
+ if (feof(stream))
return 0;
- else {
+ else
+ {
long value = 0;
- if (fread( &value, 4, 1, stream) != 1)
+ if (fread(&value, 4, 1, stream) != 1)
return 0;
return value;
@@ -347,13 +356,13 @@ read_long_from_stream( FILE *stream ) {
}
unsigned int
-read_padding_from_stream( FILE *stream, size_t bytes ) {
+read_padding_from_stream(FILE *stream, size_t bytes) {
int value[2] = {0, 0};
- if (feof( stream ))
+ if (feof(stream))
return 0;
- return fread( &value, 1, bytes, stream ) == bytes;
+ return fread(&value, 1, bytes, stream) == bytes;
}
/*
View
94 src/m0/c/m0_ops.c
@@ -21,17 +21,17 @@ Copyright (C) 2011-2012, Parrot Foundation.
if (M0_DEBUG) { \
fprintf(stderr, "pc = %d, op: " #name "\n", (char)pc); \
} \
- m0_op_##name( cf, &ops[4*pc] ); \
+ m0_op_##name(cf, &ops[4*pc]); \
}
static void
-m0_op_set_imm( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_set_imm(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = (uint64_t)(ops[2] * 256 + ops[3]);
}
static void
-m0_op_deref( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_deref(M0_CallFrame *frame, const unsigned char *ops)
{
unsigned char ref = ops[2];
@@ -56,21 +56,21 @@ m0_op_deref( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_print_s( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_print_s(M0_CallFrame *frame, const unsigned char *ops)
{
/* note the lack of filehandle selection (ops[1]) for output */
- fprintf( stdout, "%s", (char *)(frame->registers[ ops[2] ] + 8 ));
+ fprintf(stdout, "%s", (char *)(frame->registers[ ops[2] ] + 8));
}
static void
-m0_op_print_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_print_i(M0_CallFrame *frame, const unsigned char *ops)
{
/* note the lack of filehandle selection (ops[1]) for output */
- fprintf( stdout, "%d", (unsigned int)frame->registers[ ops[2] ] );
+ fprintf(stdout, "%d", (unsigned int)frame->registers[ ops[2] ]);
}
static void
-m0_op_add_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_add_i(M0_CallFrame *frame, const unsigned char *ops)
{
unsigned int r2 = *(unsigned int*)&frame->registers[ops[2]];
unsigned int r3 = *(unsigned int*)&frame->registers[ops[3]];
@@ -79,7 +79,7 @@ m0_op_add_i( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_add_n( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_add_n(M0_CallFrame *frame, const unsigned char *ops)
{
double r2 = *(double*)&frame->registers[ops[2]];
double r3 = *(double*)&frame->registers[ops[3]];
@@ -88,7 +88,7 @@ m0_op_add_n( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_sub_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_sub_i(M0_CallFrame *frame, const unsigned char *ops)
{
unsigned int r2 = *(unsigned int*)&frame->registers[ops[2]];
unsigned int r3 = *(unsigned int*)&frame->registers[ops[3]];
@@ -99,7 +99,7 @@ m0_op_sub_i( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_sub_n( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_sub_n(M0_CallFrame *frame, const unsigned char *ops)
{
double r2 = *(double*)&frame->registers[ops[2]];
double r3 = *(double*)&frame->registers[ops[3]];
@@ -108,7 +108,7 @@ m0_op_sub_n( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_convert_n_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_convert_n_i(M0_CallFrame *frame, const unsigned char *ops)
{
int64_t *r2 = (int64_t*) &(frame->registers[ops[2]]);
double *r1 = (double*) &(frame->registers[ops[1]]);
@@ -116,7 +116,7 @@ m0_op_convert_n_i( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_convert_i_n( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_convert_i_n(M0_CallFrame *frame, const unsigned char *ops)
{
double *r2 = (double*) &(frame->registers[ops[2]]);
int64_t *r1 = (int64_t*) &(frame->registers[ops[1]]);
@@ -124,20 +124,20 @@ m0_op_convert_i_n( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_goto( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_goto(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[PC] = 256 * ops[1] + ops[2];
}
static void
-m0_op_goto_if( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_goto_if(M0_CallFrame *frame, const unsigned char *ops)
{
- if( frame->registers[ops[3]] )
+ if (frame->registers[ops[3]])
frame->registers[PC] = 256 * ops[1] + ops[2];
}
static void
-m0_op_mult_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_mult_i(M0_CallFrame *frame, const unsigned char *ops)
{
unsigned int r2 = *(unsigned int*)&frame->registers[ops[2]];
unsigned int r3 = *(unsigned int*)&frame->registers[ops[3]];
@@ -146,7 +146,7 @@ m0_op_mult_i( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_mult_n( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_mult_n(M0_CallFrame *frame, const unsigned char *ops)
{
double r2 = *(double*)&frame->registers[ops[2]];
double r3 = *(double*)&frame->registers[ops[3]];
@@ -155,7 +155,7 @@ m0_op_mult_n( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_div_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_div_i(M0_CallFrame *frame, const unsigned char *ops)
{
unsigned int r2 = *(unsigned int*)&frame->registers[ops[2]];
unsigned int r3 = *(unsigned int*)&frame->registers[ops[3]];
@@ -164,7 +164,7 @@ m0_op_div_i( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_div_n( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_div_n(M0_CallFrame *frame, const unsigned char *ops)
{
double r2 = *(double*)&frame->registers[ops[2]];
double r3 = *(double*)&frame->registers[ops[3]];
@@ -173,7 +173,7 @@ m0_op_div_n( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_mod_i( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_mod_i(M0_CallFrame *frame, const unsigned char *ops)
{
unsigned int r2 = *(unsigned int*)&frame->registers[ops[2]];
unsigned int r3 = *(unsigned int*)&frame->registers[ops[3]];
@@ -182,7 +182,7 @@ m0_op_mod_i( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_mod_n( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_mod_n(M0_CallFrame *frame, const unsigned char *ops)
{
double r2 = *(double*)&frame->registers[ops[2]];
double r3 = *(double*)&frame->registers[ops[3]];
@@ -191,48 +191,48 @@ m0_op_mod_n( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_and( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_and(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = frame->registers[ops[2]] &
frame->registers[ops[3]];
}
static void
-m0_op_or( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_or(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = frame->registers[ops[2]] |
frame->registers[ops[3]];
}
static void
-m0_op_xor( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_xor(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = frame->registers[ops[2]] ^
frame->registers[ops[3]];
}
static void
-m0_op_lshr( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_lshr(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = frame->registers[ops[2]] >>
frame->registers[ops[3]];
}
static void
-m0_op_ashr( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_ashr(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = (int)frame->registers[ops[2]] >>
frame->registers[ops[3]];
}
static void
-m0_op_goto_chunk(M0_CallFrame *frame, const unsigned char *ops )
+m0_op_goto_chunk(M0_CallFrame *frame, const unsigned char *ops)
{
uint64_t new_pc = frame->registers[ops[2]];
M0_Interp *interp = (M0_Interp *)frame->registers[INTERP];
M0_Chunk *chunk = (M0_Chunk*)((*interp)[CHUNKS]);
- while(chunk) {
- if( strncmp( chunk->name, (char *)frame->registers[ops[1]], chunk->name_length) == 0) {
+ while (chunk) {
+ if (strncmp(chunk->name, (char *)frame->registers[ops[1]], chunk->name_length) == 0) {
frame->registers[CHUNK] = (uint64_t)chunk;
frame->registers[CONSTS] = (uint64_t)chunk->constants;
frame->registers[MDS] = (uint64_t)chunk->metadata;
@@ -242,26 +242,26 @@ m0_op_goto_chunk(M0_CallFrame *frame, const unsigned char *ops )
}
chunk = chunk->next;
}
- if(chunk == NULL) {
- // TODO error handling
+ if (chunk == NULL) {
+ /* TODO error handling */
}
}
static void
-m0_op_exit(M0_CallFrame *frame, const unsigned char *ops )
+m0_op_exit(M0_CallFrame *frame, const unsigned char *ops)
{
exit((int)frame->registers[ops[1]]);
}
static void
-m0_op_shl( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_shl(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = frame->registers[ops[2]] <<
frame->registers[ops[3]];
}
static void
-m0_op_set_byte( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_set_byte(M0_CallFrame *frame, const unsigned char *ops)
{
const char value = frame->registers[ops[3]];
const int offset = frame->registers[ops[2]];
@@ -270,22 +270,22 @@ m0_op_set_byte( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_set_word( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_set_word(M0_CallFrame *frame, const unsigned char *ops)
{
const char* value = (char*)&frame->registers[ops[3]];
const int offset = frame->registers[ops[2]];
char *target = (char*) frame->registers[ops[1]];
- memcpy(&target[4*offset],value,4*sizeof(char));
+ memcpy(&target[4*offset], value, 4*sizeof (char));
}
static void
-m0_op_set( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_set(M0_CallFrame *frame, const unsigned char *ops)
{
frame->registers[ops[1]] = frame->registers[ops[2]];
}
static void
-m0_op_get_byte( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_get_byte(M0_CallFrame *frame, const unsigned char *ops)
{
const char *src = (char*)frame->registers[ops[2]];
const int offset = frame->registers[ops[3]];
@@ -294,25 +294,25 @@ m0_op_get_byte( M0_CallFrame *frame, const unsigned char *ops )
}
static void
-m0_op_get_word( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_get_word(M0_CallFrame *frame, const unsigned char *ops)
{
const char *src = (char*)frame->registers[ops[2]];
const int offset = frame->registers[ops[3]];
char *target = (char*)&frame->registers[ops[1]];
frame->registers[ops[1]] = (uint64_t)0;
- memcpy(target, &src[offset * 4], 4*sizeof(char));
+ memcpy(target, &src[offset * 4], 4*sizeof (char));
}
static void
-m0_op_gc_alloc( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_gc_alloc(M0_CallFrame *frame, const unsigned char *ops)
{
const int bytes = frame->registers[ops[2]];
- void *ptr = malloc( sizeof(char) * bytes );
+ void *ptr = malloc(sizeof (char) * bytes);
frame->registers[ops[1]] = (uint64_t) ptr;
}
static void
-m0_op_set_ref( M0_CallFrame *frame, const unsigned char *ops )
+m0_op_set_ref(M0_CallFrame *frame, const unsigned char *ops)
{
uint64_t *ptr = (uint64_t *)frame->registers[ops[1]];
const int offset = frame->registers[ops[2]];
@@ -320,7 +320,7 @@ m0_op_set_ref( M0_CallFrame *frame, const unsigned char *ops )
}
int
-run_ops( M0_Interp *interp, M0_CallFrame *cf ) {
+run_ops(M0_Interp *interp, M0_CallFrame *cf) {
UNUSED(interp);
while (1) {
@@ -478,8 +478,8 @@ run_ops( M0_Interp *interp, M0_CallFrame *cf ) {
break;
default:
- fprintf( stderr, "Unimplemented op: %d (%d, %d, %d)\n",
- op, ops[4*pc + 1], ops[4*pc + 2], ops[4*pc + 3] );
+ fprintf(stderr, "Unimplemented op: %d (%d, %d, %d)\n",
+ op, ops[4*pc + 1], ops[4*pc + 2], ops[4*pc + 3]);
break;
}
}
View
36 src/m0/perl5/m0_assembler.pl
@@ -45,8 +45,8 @@ sub assemble {
my $source = slurp($file);
my $lines = [ split(/\n/, $source) ];
# build a hash that maps chunk names to their offset within the bytecode file
- my $chunk_map = {
- map { my $x = $_; $x =~ s/^\.chunk\s+"(\w+)"$/$1/; $x => our $i++; }
+ my $chunk_map = {
+ map { my $x = $_; $x =~ s/^\.chunk\s+"(\w+)"$/$1/; $x => our $i++; }
grep { m/^\.chunk\s+"(\w+)"$/ } @$lines };
my $cursor = 0;
my $version = parse_version($lines, \$cursor);
@@ -119,7 +119,7 @@ sub parse_op_data {
sub register_name_to_num {
my ($register) = @_;
- my $symbols = {
+ my $symbols = {
# call frame values
CF => 0,
PCF => 1,
@@ -233,7 +233,7 @@ sub m0b_metadata_seg {
return $bytecode;
}
-
+
sub m0b_bytecode_seg {
my ($ops, $chunk) = @_;
@@ -301,7 +301,8 @@ sub m0b_bytecode_seg {
} elsif ($line =~ m/^(?<label>[a-zA-Z][a-zA-Z0-9_]+):\s*$/) {
# ignore
} elsif ($line =~ m/^\s*#/) {
- } else {
+ } else
+ {
die "Invalid M0 bytecode line: '$line'";
}
}
@@ -464,7 +465,8 @@ sub m0b_const_seg_length {
$seg_length += $const_length;
m0_say "after adding $type constant '$value', length is $seg_length (+$const_length)";
}
- else {
+ else
+ {
die "unknown type '$type'. I don't know how this happened.";
}
}
@@ -525,11 +527,12 @@ sub parse_chunks {
if ($state eq 'none') {
if ($line =~ /^\.chunk\s+"(?<name>\w*?)"$/) {
- $chunk{name} = $+{name};
+ $chunk{name} = $+{name};
$state = 'chunk start';
m0_say "Parsing chunk #".scalar @$chunks;
}
- else {
+ else
+ {
die "Invalid M0: expected chunk name, got '$line' at line $$cursor";
}
}
@@ -537,7 +540,8 @@ sub parse_chunks {
if ($line =~ /^\.constants\s*?$/ ) {
$state = 'constants';
}
- else {
+ else
+ {
die "Invalid M0: expected constants segment start, got '$line' at line $$cursor";
}
}
@@ -568,12 +572,14 @@ sub parse_chunks {
}
$const =~ s/&/&:/;
}
- else {
+ else
+ {
die "unhandled constant type: '$const' at line $$cursor";
}
push @constants, $const;
}
- else {
+ else
+ {
die "Invalid M0: expected constants segment data or metadata segment start, got '$line' at line $$cursor";
}
}
@@ -584,7 +590,8 @@ sub parse_chunks {
elsif ($line =~ /(\d+)\s+(\d+)\s+(\d+)\s*$/) {
push @metadata, $line;
}
- else {
+ else
+ {
die "Invalid M0: expected metadata segment data or bytecode segment start, got '$line' at line $$cursor";
}
}
@@ -596,13 +603,14 @@ sub parse_chunks {
push @$chunks, {%chunk};
(@constants, @metadata, @bytecode) = ((),(),());
%chunk = ();
- $chunk{name} = $+{name};
+ $chunk{name} = $+{name};
$state = 'chunk start';
}
elsif ($line =~ /^[^\.].*/) {
push @bytecode, $line;
}
- else {
+ else
+ {
die "Invalid M0: expected bytecode segment data or start of new chunk, got '$line' at line $$cursor";
}
}
View
18 src/m0/perl5/m0_interp.pl
@@ -3,7 +3,7 @@
=head1 NAME
-src/m0/m0_interpreter.pl - M0 Interpreter Prototype
+src/m0/m0_interpreter.pl - M0 Interpreter Prototype
=head1 SYNOPSIS
@@ -145,7 +145,7 @@ sub new_interp {
\&m0_opfunc_print_i,
\&m0_opfunc_print_n,
\&m0_opfunc_exit,
-# end_gen
+# end_gen
];
$interp->[CONFIG] = {};
$interp->[CALL_FRAMES] = [];
@@ -205,7 +205,7 @@ sub new_call_frame{
sub run_ops {
my ($cf) = @_;
-
+
while (1) {
my $init_pc = i($cf,PC);
my $instr_count = scalar(@{$cf->[BCS]});
@@ -471,7 +471,7 @@ sub m0_opfunc_copy_mem {
sub m0_opfunc_set {
my ($cf, $a1, $a2, $a3) = @_;
m0_say "set $a1, $a2, $a3";
-
+
$$cf->[$a1] = $$cf->[$a2];
$$cf = $$cf->[CF];
}
@@ -500,7 +500,7 @@ sub m0_opfunc_set_ref {
sub m0_opfunc_set_byte {
my ($cf, $a1, $a2, $a3) = @_;
m0_say "set_byte $a1, $a2, $a3";
-
+
$$cf->[$a1] = bytes::substr($$cf->[$a2], i($$cf,$a3));
my $new_byte = bytes::chr($$cf->[$a3] & 255);
bytes::substr($$cf->[$a2], i($$cf,$a3), $new_byte);
@@ -509,14 +509,14 @@ sub m0_opfunc_set_byte {
sub m0_opfunc_get_byte {
my ($cf, $a1, $a2, $a3) = @_;
m0_say "get_byte $a1, $a2, $a3";
-
+
$$cf->[$a1] = i(bytes::ord(bytes::substr($$cf->[$a2], i($$cf,$a3), 1)))
}
sub m0_opfunc_set_word {
my ($cf, $a1, $a2, $a3) = @_;
m0_say "set_word $a1, $a2, $a3";
-
+
#turn *$3 into 4 bytes
my $word = i($$cf,$a3);
bytes::substr($$cf->[$a1], 4 * i($$cf,$a2), 4, $word);
@@ -525,7 +525,7 @@ sub m0_opfunc_set_word {
sub m0_opfunc_get_word {
my ($cf, $a1, $a2, $a3) = @_;
m0_say "get_word $a1, $a2, $a3";
-
+
#$$cf->[$a1] = unpack("l", bytes::substr($$cf->[$a2], 4 * $$cf->[$a3], 4));
$$cf->[$a1] = bytes::substr($$cf->[$a2], 4 * i($$cf,$a3), 4);
}
@@ -616,7 +616,7 @@ sub parse_m0b_header {
if ($real_magic cmp $m0b_magic) {
die "magic number mismatch";
}
-
+
# verify that the interp understands this version of the m0b format
my $m0b_version = ord get_bytes($m0b, $cursor, 1);
if ($m0b_version != 0) {
View
42 t/m0/m0_bytecode_loading.t
@@ -41,7 +41,7 @@ my $M0_BC_SEG = 0x04;
my $m0_interp = $ENV{M0_INTERP} || "$^X " . catfile(qw/src m0 perl5 m0_interp.pl/);
my $m0b_data = [
- {
+ {
name => 'chunk_0',
vars => [],
meta => {},
@@ -54,8 +54,8 @@ can_parse($m0_interp, $m0b_data, "very minimal bytecode file" );
$m0b_data = [
- {
- name => 'chunk_0',
+ {
+ name => 'chunk_0',
vars => [ qw(quux flub buuuz) ],
vars => [],
meta => {},
@@ -67,8 +67,8 @@ can_parse($m0_interp, $m0b_data, "minimal bytecode with some variables" );
$m0b_data = [
- {
- name => 'chunk_0',
+ {
+ name => 'chunk_0',
vars => [],
meta => { 0 => {line => 3, file => 'awesome'},
3 => {cow => 4},
@@ -81,8 +81,8 @@ can_parse($m0_interp, $m0b_data, "minimal bytecode file with a metadata segment"
$m0b_data = [
- {
- name => 'chunk_0',
+ {
+ name => 'chunk_0',
vars => [ qw(quux flub buuuz) ],
meta => { 0 => {line => 3, file => 'awesome'},
3 => {cow => 4},
@@ -95,11 +95,11 @@ can_parse($m0_interp, $m0b_data, "bytecode file with metadata and vars" );
$m0b_data = [
- {
- name => 'chunk_0',
+ {
+ name => 'chunk_0',
vars => [ ],
meta => { },
- bc => 99,
+ bc => 99,
},
];
@@ -107,8 +107,8 @@ can_parse($m0_interp, $m0b_data, "bytecode file with null ops" );
$m0b_data = [
- {
- name => 'chunk_0',
+ {
+ name => 'chunk_0',
vars => [ qw(quux flub buuuz) ],
meta => { 0 => {line => 3, file => 'awesome'},
3 => {cow => 4},
@@ -121,24 +121,24 @@ can_parse($m0_interp, $m0b_data, "bytecode file with metadata, vars and null ops
$m0b_data = [
- {
- name => 'chunk_0',
+ {
+ name => 'chunk_0',
vars => [ qw(quux flub buuuz) ],
meta => { 0 => {line => 3, file => 'awesome'},
3 => {cow => 4},
},
bc => 11, # number of ops to generate (all null)
},
- {
- name => 'chunk_2',
+ {
+ name => 'chunk_2',
vars => [ qw(quux2 flub2 buuuz2) ],
meta => { 0 => {line2 => 32, file2 => 'awesome2'},
3 => {cow2 => 42},
},
bc => 22, # number of ops to generate (all null)
},
- {
- name => 'chunk_3',
+ {
+ name => 'chunk_3',
vars => [ qw(quux3 flub3 buuuz3) ],
meta => { 03 => {line3 => 33, file3 => 'awesome3'},
33 => {cow3 => 43},
@@ -347,7 +347,7 @@ sub m0b_bc_seg_length {
Generate the binary version of an M0 variables segment, using the variables in
the first argument.
-NOTE: Currently this generates an empty variables segment.
+NOTE: Currently this generates an empty variables segment.
=cut
@@ -403,7 +403,7 @@ NOTE: Currently generates an empty metadata segment.
sub m0b_meta_seg {
my ($metadata, $var_seg) = @_;
-
+
my $entry_count = 0;
for my $offset (keys %$metadata) {
$entry_count += keys %{$metadata->{$offset}};
@@ -437,7 +437,7 @@ Calculate the number of bytes that a metadata segment will occupy.
sub m0b_meta_seg_length {
my ($metadata) = @_;
-
+
my $seg_length = 12; # 4 for segment identifier, 4 for count, 4 for size
for my $offset (keys %$metadata) {
Please sign in to comment.
Something went wrong with that request. Please try again.