Example #1
0
long lzw_encode(char *source,void *target,int size)
  {
  long bitpos = 0;
  DOUBLE_S p;
  int f;

  clear:
  old_value = p.group = *source++;size--;
  while (size-->0)
     {
     p.chr = (int)((char)(*source++-old_value));old_value += p.chr;
     f = find_code(&p);
     if (f<0)
        {
        bitpos = output_code(target,bitpos,bitsize,p.group);
        add_code(&p);
        if (nextgroup == (1<<bitsize)) bitsize++;
        p.group = p.chr;
        if (nextgroup>= LZW_MAX_CODES)
           {
           bitpos = output_code(target,bitpos,bitsize,p.group);
           bitpos = output_code(target,bitpos,bitsize,clear_code);
           do_clear_code();
           goto clear;
           }
        }
     else
        p.group = f;
     }
  bitpos = output_code(target,bitpos,bitsize,p.group);
  bitpos = output_code(target,bitpos,bitsize,end_code);
  return (bitpos+8)>>3;
  }
Example #2
0
/*
	This is the compression routine.  The code should be a fairly close
	match to the algorithm accompanying the article.
*/
void compress(FILE * input,FILE * output) {
	unsigned int next_code;
	unsigned int character;
	unsigned int string_code;
	unsigned int index;
	int i;
	
	/* Write original file length */
	long int flen = filelength(fileno(input));
	putc((unsigned char)(flen >> 24),output);
	putc((unsigned char)(flen >> 16),output);
	putc((unsigned char)(flen >> 8),output);
	putc((unsigned char)flen,output);			

	next_code=256;              /* Next code is the next available string code*/
	for (i=0;i<TABLE_SIZE;i++)  /* Clear out the string table before starting */
    	code_value[i]=-1;

	i=0;
	string_code=getc(input);    /* Get the first code */
	
	/*
	   This is the main loop where it all happens.  This loop runs util all of
	   the input has been exhausted.  Note that it stops adding codes to the
	   table after all of the possible codes have been defined.
	*/
	while ((character=getc(input)) != (unsigned)EOF) {
    
		if (++i==1000) {
			i=0;
			printf("*");
		}
		
		index=find_match(string_code,character);	/* See if the string is in */
		if (code_value[index] != -1)				/* the table.  If it is,   */
			string_code=code_value[index];        	/* get the code value.  If */
		else                                    	/* the string is not in the*/
		{                                       	/* table, try to add it.   */
			if (next_code <= MAX_CODE) {
				code_value[index]=next_code++;
				prefix_code[index]=string_code;
				append_character[index]=character;
			}
			output_code(output,string_code);	/* When a string is found  */
			string_code=character;				/* that is not in the table*/
		}										/* I output the last string*/
	}											/* after adding the new one*/
	
	/* End of the main loop. */
	output_code(output,string_code); /* Output the last code */
	output_code(output,MAX_VALUE);   /* Output the end of buffer code */
	output_code(output,0);           /* This code flushes the output buffer */
}
Example #3
0
void output_start(char *outfilename)
{
    outf = fopen(outfilename, "w");
    if (outf == NULL) {
	perror(outfilename);
	exit(EXIT_FAILURE);
    }

#if 0 /* we'll let the host program set the stack */
    output_code("LD", "SP, %d", STACKTOP);
#endif
    output_code("LD", "A, 0");
    output_code("LD", "(SHOULD_RUN), A");
    output_code("CALL", "_main");
    output_code("HALT", "");
    output_blank();
}
Example #4
0
void Telegraph::send_message(const char* message) {
    unsigned int message_length = (unsigned int)strlen(message);
    for (unsigned int i = 0; i < message_length; i++) {
        const char current_char = toupper(message[i]);

        if (isalpha(current_char)) {
            output_code(LETTERS[current_char - 'A']);
            delay(_dah_length);
        } else if (isdigit(current_char)) {
            output_code(DIGITS[current_char - '0']);
            delay(_dah_length);
        } else if (current_char == ' ') {
            Serial.print(" ");
            delay(_dit_length * 7);
        }
    }

    Serial.println();
}
Example #5
0
/*
 * Performs one subordinate pass.
 */
void subordinate_pass(element_type threshold)
{
	list_type d;    //定义一颗零树
	int i;
	char found;
	if (threshold>0) 
	{
		for (i=0; i<list_length; i++) 
		{
			d = get_list_element(i,&found);
			if (found==1) 
			{
				if ((d.x&threshold)!=0)
					output_code(ONE);
				else 
					output_code(ZERO);
			}
		}
	}
}
Example #6
0
void compress(FILE *input,FILE *output)
{
unsigned int next_code;
unsigned int character;
unsigned int string_code;
unsigned int index;
int i;
    next_code=256;
    for (i=0;i<TABLE_SIZE;i++)
    code_value[i]=-1;
    i=0;
    printf("Compressing...\n");
    string_code=getc(input);


    while ((character=getc(input)) != (unsigned)EOF)
    {
	index=find_match(string_code,character);
	if (code_value[index] != -1)
	    string_code=code_value[index];
	else
	{
	    if (next_code <= MAX_CODE)
	    {
		code_value[index]=next_code++;
		prefix_code[index]=string_code;
		append_character[index]=character;
	    }
	    output_code(output,string_code);
	    string_code=character;
	}
    }

    output_code(output,string_code);
    output_code(output,MAX_VALUE);
    output_code(output,0);
    printf("\n");
}
Example #7
0
int
output_parameters(NODE *node, int break_label, int continue_label)
{
    int stack_size;

    stack_size = 0;

    if (node != NULL) {
	if (node->op == FUNCTION_PARAM) {
	    stack_size += output_parameters(node->arg[1], break_label, continue_label);
	    stack_size += output_parameters(node->arg[0], break_label, continue_label);
	} else {
	    output_node(node, break_label, continue_label);
	    output_code("PUSH", "HL");
	    stack_size = 2;
	}
    }

    return stack_size;
}
Example #8
0
int
main(int argc, char *argv[])
{
	int  ch;
	int  retval;
	char *inputfilename;
	scope_t *sentinal;

	STAILQ_INIT(&patches);
	SLIST_INIT(&search_path);
	STAILQ_INIT(&seq_program);
	TAILQ_INIT(&cs_tailq);
	SLIST_INIT(&scope_stack);

	/* Set Sentinal scope node */
	sentinal = scope_alloc();
	sentinal->type = SCOPE_ROOT;
	
	includes_search_curdir = 1;
	appname = *argv;
	regfile = NULL;
	listfile = NULL;
#if DEBUG
	yy_flex_debug = 0;
	mm_flex_debug = 0;
	yydebug = 0;
	mmdebug = 0;
#endif
	while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:X")) != -1) {
		switch(ch) {
		case 'd':
#if DEBUG
			if (strcmp(optarg, "s") == 0) {
				yy_flex_debug = 1;
				mm_flex_debug = 1;
			} else if (strcmp(optarg, "p") == 0) {
				yydebug = 1;
				mmdebug = 1;
			} else {
				fprintf(stderr, "%s: -d Requires either an "
					"'s' or 'p' argument\n", appname);
				usage();
			}
#else
			stop("-d: Assembler not built with debugging "
			     "information", EX_SOFTWARE);
#endif
			break;
		case 'i':
			stock_include_file = optarg;
			break;
		case 'l':
			/* Create a program listing */
			if ((listfile = fopen(optarg, "w")) == NULL) {
				perror(optarg);
				stop(NULL, EX_CANTCREAT);
			}
			listfilename = optarg;
			break;
		case 'n':
			/* Don't complain about the -nostdinc directrive */
			if (strcmp(optarg, "ostdinc")) {
				fprintf(stderr, "%s: Unknown option -%c%s\n",
					appname, ch, optarg);
				usage();
				/* NOTREACHED */
			}
			break;
		case 'o':
			if ((ofile = fopen(optarg, "w")) == NULL) {
				perror(optarg);
				stop(NULL, EX_CANTCREAT);
			}
			ofilename = optarg;
			break;
		case 'p':
			/* Create Register Diagnostic "printing" Functions */
			if ((regdiagfile = fopen(optarg, "w")) == NULL) {
				perror(optarg);
				stop(NULL, EX_CANTCREAT);
			}
			regdiagfilename = optarg;
			break;
		case 'r':
			if ((regfile = fopen(optarg, "w")) == NULL) {
				perror(optarg);
				stop(NULL, EX_CANTCREAT);
			}
			regfilename = optarg;
			break;
		case 'I':
		{
			path_entry_t include_dir;

			if (strcmp(optarg, "-") == 0) {
				if (includes_search_curdir == 0) {
					fprintf(stderr, "%s: Warning - '-I-' "
							"specified multiple "
							"times\n", appname);
				}
				includes_search_curdir = 0;
				for (include_dir = SLIST_FIRST(&search_path);
				     include_dir != NULL;
				     include_dir = SLIST_NEXT(include_dir,
							      links))
					/*
					 * All entries before a '-I-' only
					 * apply to includes specified with
					 * quotes instead of "<>".
					 */
					include_dir->quoted_includes_only = 1;
			} else {
				include_dir =
				    (path_entry_t)malloc(sizeof(*include_dir));
				if (include_dir == NULL) {
					perror(optarg);
					stop(NULL, EX_OSERR);
				}
				include_dir->directory = strdup(optarg);
				if (include_dir->directory == NULL) {
					perror(optarg);
					stop(NULL, EX_OSERR);
				}
				include_dir->quoted_includes_only = 0;
				SLIST_INSERT_HEAD(&search_path, include_dir,
						  links);
			}
			break;
		}
		case 'X':
			/* icc version of -nostdinc */
			break;
		case '?':
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc != 1) {
		fprintf(stderr, "%s: No input file specifiled\n", appname);
		usage();
		/* NOTREACHED */
	}

	if (regdiagfile != NULL
	 && (regfile == NULL || stock_include_file == NULL)) {
		fprintf(stderr,
			"%s: The -p option requires the -r and -i options.\n",
			appname);
		usage();
		/* NOTREACHED */
	}
	symtable_open();
	inputfilename = *argv;
	include_file(*argv, SOURCE_FILE);
	retval = yyparse();
	if (retval == 0) {
		if (SLIST_FIRST(&scope_stack) == NULL
		 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
			stop("Unterminated conditional expression", EX_DATAERR);
			/* NOTREACHED */
		}

		/* Process outmost scope */
		process_scope(SLIST_FIRST(&scope_stack));
		/*
		 * Decend the tree of scopes and insert/emit
		 * patches as appropriate.  We perform a depth first
		 * tranversal, recursively handling each scope.
		 */
		/* start at the root scope */
		dump_scope(SLIST_FIRST(&scope_stack));

		/* Patch up forward jump addresses */
		back_patch();

		if (ofile != NULL)
			output_code();
		if (regfile != NULL)
			symtable_dump(regfile, regdiagfile);
		if (listfile != NULL)
			output_listing(inputfilename);
	}

	stop(NULL, 0);
	/* NOTREACHED */
	return (0);
}
Example #9
0
File: orcc.c Project: mojaves/orc
int
main (int argc, char *argv[])
{
  char *code;
  int i;
  char *output_file = NULL;
  char *input_file = NULL;
  char *include_file = NULL;
  char *compat_version = VERSION;
  FILE *output;
  OrcParseError **errors = NULL;
  int n_errors = 0;

  orc_init ();

  for(i=1;i<argc;i++) {
    if (strcmp(argv[i], "--header") == 0) {
      mode = MODE_HEADER;
    } else if (strcmp(argv[i], "--implementation") == 0) {
      mode = MODE_IMPL;
    } else if (strcmp(argv[i], "--test") == 0) {
      mode = MODE_TEST;
    } else if (strcmp(argv[i], "--assembly") == 0) {
      mode = MODE_ASSEMBLY;
    } else if (strcmp(argv[i], "--parse-only") == 0) {
      mode = MODE_PARSE;
    } else if (strcmp(argv[i], "--include") == 0) {
      if (i+1 < argc) {
        include_file = argv[i+1];
        i++;
      } else {
        help();
      }
    } else if (strcmp (argv[i], "--output") == 0 ||
        strcmp(argv[i], "-o") == 0) {
      if (i+1 < argc) {
        output_file = argv[i+1];
        i++;
      } else {
        help();
      }
    } else if (strcmp(argv[i], "--target") == 0 ||
        strcmp(argv[i], "-t") == 0) {
      if (i+1 < argc) {
        target = argv[i+1];
        i++;
      } else {
        help();
      }
    } else if (strcmp(argv[i], "--inline") == 0) {
      use_inline = TRUE;
    } else if (strcmp(argv[i], "--no-inline") == 0) {
      use_inline = FALSE;
    } else if (strcmp(argv[i], "--internal") == 0) {
      use_internal = TRUE;
    } else if (strcmp(argv[i], "--no-internal") == 0) {
      use_internal = FALSE;
    } else if (strcmp(argv[i], "--init-function") == 0) {
      if (i+1 < argc) {
        init_function = argv[i+1];
        i++;
      } else {
        help();
      }
    } else if (strcmp(argv[i], "--help") == 0 ||
        strcmp(argv[i], "-h") == 0) {
      help ();
    } else if (strcmp(argv[i], "--verbose") == 0 ||
        strcmp(argv[i], "-v") == 0) {
      verbose = 1;
    } else if (strcmp(argv[i], "--version") == 0) {
      fprintf(stderr, "Orc Compiler " PACKAGE_VERSION "\n");
      exit (0);
    } else if (strcmp(argv[i], "--compat") == 0) {
      if (i+1 < argc) {
        compat_version = argv[i+1];
        i++;
      } else {
        help();
      }
    } else if (strcmp(argv[i], "--lazy-init") == 0) {
      use_lazy_init = TRUE;
    } else if (strcmp(argv[i], "--no-backup") == 0) {
      use_backup = FALSE;
    } else if (strncmp(argv[i], "-", 1) == 0) {
      fprintf(stderr, "Unknown option: %s\n", argv[i]);
      exit (1);
    } else {
      if (input_file == NULL) {
        input_file = argv[i];
      } else {
        fprintf(stderr, "More than one input file specified: %s\n", argv[i]);
        exit (1);
      }
    }
  }

  if (input_file == NULL) {
    fprintf(stderr, "No input file specified\n");
    exit (1);
  }

  if (mode == MODE_ASSEMBLY && orc_target_get_by_name (target) == NULL) {
    fprintf(stderr, "Unknown target \"%s\"\n", target);
    exit (1);
  }

  if (compat_version) {
    int major, minor, micro, nano = 0;
    int n;

    n = sscanf (compat_version, "%d.%d.%d.%d", &major, &minor, &micro, &nano);

    if (n < 3) {
      fprintf(stderr, "Unknown version \"%s\"\n", compat_version);
      exit (1);
    }

    compat = ORC_VERSION(major,minor,micro,nano);
    if (compat < ORC_VERSION(0,4,5,0)) {
      fprintf(stderr, "Compatibility version \"%s\" not supported.  Minimum 0.4.5\n",
          compat_version);
      exit (1);
    }
  }
  if (compat >= ORC_VERSION(0,4,11,1)) {
    use_code = TRUE;
  }

  if (output_file == NULL) {
    switch (mode) {
      case MODE_IMPL:
        output_file = "out.c";
        break;
      case MODE_HEADER:
        output_file = "out.h";
        break;
      case MODE_TEST:
        output_file = "out_test.c";
        break;
      case MODE_ASSEMBLY:
        output_file = "out.s";
        break;
    }
  }

  code = read_file (input_file);
  if (!code) {
    fprintf(stderr, "Could not read input file: %s\n", input_file);
    exit(1);
  }

  orc_parse_code (code, &programs, &n_programs, &errors, &n_errors);
  if (n_errors > 0) {
    int i;
    for (i=0;i<n_errors;i++) {
      fprintf(stderr, "%s @ %i: error: %s\n", errors[i]->source, errors[i]->line_number, errors[i]->text);
    }
    exit (1);
  }

  if (programs == NULL) {
    if (verbose) {
      fprintf(stderr, "no programs found\n");
    }
    exit(1);
  }

  if (verbose) {
    fprintf(stderr, "%i program%s parsed\n",
           n_programs, (n_programs > 1) ?"s" :"");
  }

  if (mode == MODE_PARSE) {
    exit (0);
  }

  if (init_function == NULL) {
    init_function = orc_parse_get_init_function (programs[0]);
  }

  if (init_function == NULL) {
    use_lazy_init = TRUE;
  }

  output = fopen (output_file, "w");
  if (!output) {
    fprintf(stderr, "Could not write output file: %s\n", output_file);
    exit(1);
  }

  fprintf(output, "\n");
  fprintf(output, "/* autogenerated from %s */\n", my_basename(input_file));
  fprintf(output, "\n");

  if (mode == MODE_IMPL) {
    fprintf(output, "#ifdef HAVE_CONFIG_H\n");
    fprintf(output, "#include \"config.h\"\n");
    fprintf(output, "#endif\n");
    if (include_file) {
      fprintf(output, "#include <%s>\n", include_file);
    }
    fprintf(output, "\n");
    fprintf(output, "%s", orc_target_c_get_typedefs ());
    fprintf(output, "\n");
    fprintf(output, "#ifndef DISABLE_ORC\n");
    fprintf(output, "#include <orc/orc.h>\n");
    fprintf(output, "#endif\n");
    for(i=0;i<n_programs;i++){
      output_code_header (programs[i], output);
    }
    if (init_function) {
      fprintf(output, "\n");
      fprintf(output, "void %s (void);\n", init_function);
    }
    fprintf(output, "\n");
    fprintf(output, "%s", orc_target_get_asm_preamble ("c"));
    fprintf(output, "\n");
    for(i=0;i<n_programs;i++){
      output_code (programs[i], output);
    }
    fprintf(output, "\n");
    if (init_function) {
      output_init_function (output);
      fprintf(output, "\n");
    }
  } else if (mode == MODE_HEADER) {
    char *barrier = get_barrier (output_file);

    fprintf(output, "#ifndef _%s_\n", barrier);
    fprintf(output, "#define _%s_\n", barrier);
    free (barrier);
    fprintf(output, "\n");
    if (include_file) {
      fprintf(output, "#include <%s>\n", include_file);
    }
    fprintf(output, "\n");
    fprintf(output, "#ifdef __cplusplus\n");
    fprintf(output, "extern \"C\" {\n");
    fprintf(output, "#endif\n");
    fprintf(output, "\n");
    if (init_function) {
      fprintf(output, "void %s (void);\n", init_function);
      fprintf(output, "\n");
    }
    fprintf(output, "\n");
    if (!use_inline) {
      fprintf(output, "\n");
      fprintf(output, "%s", orc_target_c_get_typedefs ());
      for(i=0;i<n_programs;i++){
        output_code_header (programs[i], output);
      }
    } else {
      fprintf(output, "\n");
      fprintf(output, "#include <orc/orc.h>\n");
      fprintf(output, "\n");
      for(i=0;i<n_programs;i++){
        output_code_execute (programs[i], output, TRUE);
      }
    }
    fprintf(output, "\n");
    fprintf(output, "#ifdef __cplusplus\n");
    fprintf(output, "}\n");
    fprintf(output, "#endif\n");
    fprintf(output, "\n");
    fprintf(output, "#endif\n");
    fprintf(output, "\n");
  } else if (mode == MODE_TEST) {
    fprintf(output, "#include <stdio.h>\n");
    fprintf(output, "#include <string.h>\n");
    fprintf(output, "#include <stdlib.h>\n");
    fprintf(output, "#include <math.h>\n");
    if (include_file) {
      fprintf(output, "#include <%s>\n", include_file);
    }
    fprintf(output, "\n");
    fprintf(output, "%s", orc_target_c_get_typedefs ());
    fprintf(output, "#include <orc/orc.h>\n");
    fprintf(output, "#include <orc-test/orctest.h>\n");
    fprintf(output, "%s", orc_target_get_asm_preamble ("c"));
    fprintf(output, "\n");
    if (use_backup) {
      for(i=0;i<n_programs;i++){
        fprintf(output, "/* %s */\n", programs[i]->name);
        output_code_backup (programs[i], output);
      }
    }
    fprintf(output, "\n");
    fprintf(output, "static int quiet = 0;\n");
    fprintf(output, "static int benchmark = 0;\n");
    fprintf(output, "\n");
    fprintf(output, "static void help (const char *argv0)\n");
    fprintf(output, "{\n");
    fprintf(output, "  fprintf(stderr, \"Usage:\\n\");\n");
    fprintf(output, "  fprintf(stderr, \"  %%s [OPTION]\\n\", argv0);\n");
    fprintf(output, "  fprintf(stderr, \"Help Options:\\n\");\n");
    fprintf(output, "  fprintf(stderr, \"  -h, --help          Show help options\\n\");\n");
    fprintf(output, "  fprintf(stderr, \"Application Options:\\n\");\n");
    fprintf(output, "  fprintf(stderr, \"  -b, --benchmark     Run benchmark and show results\\n\");\n");
    fprintf(output, "  fprintf(stderr, \"  -q, --quiet         Don't output anything except on failures\\n\");\n");
    fprintf(output, "\n");
    fprintf(output, "  exit(0);\n");
    fprintf(output, "}\n");
    fprintf(output, "\n");
    fprintf(output, "int\n");
    fprintf(output, "main (int argc, char *argv[])\n");
    fprintf(output, "{\n");
    fprintf(output, "  int error = FALSE;\n");
    fprintf(output, "  int i;\n");
    fprintf(output, "\n");
    fprintf(output, "  orc_test_init ();\n");
    fprintf(output, "\n");
    fprintf(output, "  for(i=1;i<argc;i++) {\n");
    fprintf(output, "    if (strcmp(argv[i], \"--help\") == 0 ||\n");
    fprintf(output, "      strcmp(argv[i], \"-h\") == 0) {\n");
    fprintf(output, "      help(argv[0]);\n");
    fprintf(output, "    } else if (strcmp(argv[i], \"--quiet\") == 0 ||\n");
    fprintf(output, "      strcmp(argv[i], \"-q\") == 0) {\n");
    fprintf(output, "      quiet = 1;\n");
    fprintf(output, "      benchmark = 0;\n");
    fprintf(output, "    } else if (strcmp(argv[i], \"--benchmark\") == 0 ||\n");
    fprintf(output, "      strcmp(argv[i], \"-b\") == 0) {\n");
    fprintf(output, "      benchmark = 1;\n");
    fprintf(output, "      quiet = 0;\n");
    fprintf(output, "    }\n");
    fprintf(output, "  }\n");
    fprintf(output, "\n");
    for(i=0;i<n_programs;i++){
      output_code_test (programs[i], output);
    }
    fprintf(output, "\n");
    fprintf(output, "  if (error) {\n");
    fprintf(output, "    return 1;\n");
    fprintf(output, "  };\n");
    fprintf(output, "  return 0;\n");
    fprintf(output, "}\n");
  } else if (mode == MODE_ASSEMBLY) {
    fprintf(output, "%s", orc_target_get_asm_preamble (target));
    for(i=0;i<n_programs;i++){
      output_code_assembly (programs[i], output);
    }
  }

  fclose (output);

  if (error) {
    remove (output_file);
    exit(1);
  }

  return 0;
}
Example #10
0
int main(int argc, char **argv)
{
	char *outfile=NULL;
	int read=0, write=0, output=0, list=0, c, index;
	const struct option long_options[] = {
		{"list",	no_argument,		NULL,	'l'},
		{"read",	no_argument,		NULL,	'r'},
		{"write",	no_argument,		NULL,	'w'},
		{"output",	required_argument,	NULL,	'o'},
		{"help",	no_argument,		NULL,	'h'},
		{"version",	no_argument,		NULL,	'v'},
		{NULL, 0, NULL, 0}
	};
	while ((c = getopt_long(argc, argv, "lrwho:", long_options, &index)) != -1) {
		switch (c) {
		case 'l':
			list = 1;
			break;
		case 'r':
			read = 1;
			break;
		case 'w':
			write = 1;
			break;
		case 'h':
			usage(stdout);
			return 0;
		case 'o':
			output = 1;
			outfile = optarg;
			break;
		case 'v':
			version();
			return 0;
		default:
			usage(stderr);
			return -1;
		}
	}
	if (write+read+list > 1) {
		fputs("Conflicting actions given simultaneously.\n", stderr);
		usage(stderr);
		return -1;
	} else if (!write+read+list) {
		write = 1;
	}
	if (output && !write) {
		fputs("It only makes sense to use --output with --write.\n", stderr);
		usage(stderr);
		return 1;
	}
	if (list) {
		list_sections();
	} else if (read) {
		FILE *f=NULL;
		for (index = optind; index < argc; index++) {
			f = open_file(argv[index], "r");
			run_code(argv[index], f);
			if (fclose(f))
				file_error(argv[index], "closing");
		}
		/* If we haven't opened anything, use stdin */
		if (!f)
			run_code("stdin", stdin);
	} else {
		FILE *f;
		if (outfile)
			f = open_file(outfile, "w");
		else
			f = stdout;
		if (optind < argc) {
			fputs("--write takes no arguments\n", stderr);
			usage(stderr);
			return -1;
		}
		create_code();
		output_code(f);
	}
	return 0;
}
Example #11
0
/*
 * Performs one complete dominant pass. Dominant-pass-codes are sent to the
 * output stream and the subordinate list is updated.
 */
void dominant_pass(matrix_2d *m, element_type threshold)
{
	ezw_element s;
	int min_x, max_x, min_y, max_y;
	//将当前扫描位置移至example[0][0]处,并进行判断、量化
	s.x = 0;
	s.y = 0;
	//将该系数与当前阈值进行比较,符号化
	process_element(m,threshold,&s);
	//将上一步的结果进行输出编码
	output_code(s.code);
	//对example[1][0],example[0][1],example[1][1]进行判断、量化
	s.x = 1;
	s.y = 0;
	//将该系数与当前阈值进行比较,符号化
	process_element(m,threshold,&s);
	//将上一步的结果放到扫描序列中
	put_in_fifo(s);
	s.x = 0;
	s.y = 1;
	//将该系数与当前阈值进行比较,符号化
	process_element(m,threshold,&s);
	//将上一步的结果放到扫描序列中
	put_in_fifo(s);
	s.x = 1;
	s.y = 1;
	//将该系数与当前阈值进行比较,符号化
	process_element(m,threshold,&s);
	//将上一步的结果放到扫描序列中
	put_in_fifo(s);
	//从扫描序列中取出一个系数
	s = get_from_fifo();
	//假如序列为空,直接将其送输出编码
	if (fifo_empty==0) 
		output_code(s.code);
	while (fifo_empty==0) 
	{
		//如果当前编码不为ZTR,则将当前扫描移至下一频段
		if (s.code!=ZTR) 
		{
			//横坐标最小值左移1位,即加倍
			min_x = s.x << 1;
			//横坐标最大值为最小值加1
			max_x = min_x+1;
			//纵坐标最小值左移1位,即加倍
			min_y = s.y << 1;
			//纵坐标最大值为最小值加1
			max_y = min_y+1;
			if ((max_x<=m->col) && (max_y<=m->row)) 
			{
				for (s.y=min_y; s.y<max_y+1; s.y++) 
				{
					for (s.x=min_x; s.x<max_x+1; s.x++) 
					{
						process_element(m,threshold,&s);
						put_in_fifo(s);
					}
				}
			}
		}
		s = get_from_fifo();
		if (fifo_empty==0) output_code(s.code);
	}
}
Example #12
0
void output_node(NODE *node, int break_label, int continue_label)
{
    NODE *n;
    int top, cont, out, bottom, not, tmp1, imm, state;
    int stack_size;
    static int last_line = -1;

    if (node == NULL) {
	return;
    }

    // In case we call yyerror().
    yylineno = node->line;

    if (node->line != last_line && node->op != ';' && node->op != FOR) {
	last_line = node->line;

	/* fprintf(outf, "%d, %s\n", node->op, get_op_name(node->op)); */
	output_line(node->line);
    }

    switch (node->op) {
	case NUMBER:
	    output_code("LD", "HL, %d", node->data.value);
	    break;
	case STRING:
	    output_code("LD", "HL, %s", node->data.address);
	    break;
	case IDENT:
	    if (node->data.var->scope == SCOPE_GLOBAL) {
		if (node->lhs) {
		    output_code("LD", "IX, _%s", node->data.var->name);
		} else if (node->data.var->decl->decl_type == DECL_ARRAY) {
		    output_code("LD", "HL, _%s", node->data.var->name);
		} else {
		    output_code("LD", "HL, (_%s)", node->data.var->name);
		}
	    } else {
		if (node->lhs) {
		    output_code("PUSH", "IY");
		    output_code("POP", "IX");
		    output_code("LD", "BC, %d", node->data.var->offset);
		    output_code("ADD", "IX, BC");
		} else if (node->data.var->decl->decl_type == DECL_ARRAY) {
		    output_code("LD", "BC, %d", node->data.var->offset);
		    output_code("PUSH", "IY");
		    output_code("POP", "HL");
		    output_code("ADD", "HL, BC");
		} else {
		    output_code("LD", "L, (IY + %d)", node->data.var->offset);
		    output_code("LD", "H, (IY + %d)",
			node->data.var->offset + 1);
		}
	    }
	    break;
	case '(':
	    /* limited function call support */
	    stack_size = output_parameters(node->arg[1], break_label, continue_label);
	    output_code("CALL", "_%s", node->arg[0]->data.var->name);
	    while (stack_size-- > 0) {
		output_code("INC", "SP");
	    }
	    break;
	case INCR:
	    output_node(node->arg[0], break_label, continue_label); /* Address is in IX */
	    if (node->data.detail == -1) {  /* Preincrement */
		output_code("LD", "L, (IX + 0)");
		output_code("LD", "H, (IX + 1)");
		output_code("INC", "HL");
		output_code("LD", "(IX + 0), L");
		output_code("LD", "(IX + 1), H");
	    } else {  /* Postincrement */
		output_code("LD", "L, (IX + 0)");
		output_code("LD", "H, (IX + 1)");
		output_code("LD", "E, L");
		output_code("LD", "D, H");
		output_code("INC", "DE");
		output_code("LD", "(IX + 0), E");
		output_code("LD", "(IX + 1), D");
	    }
	    break;
	case DECR:
	    output_node(node->arg[0], break_label, continue_label); /* Address is in IX */
	    if (node->data.detail == -1) {  /* Predecrement */
		output_code("LD", "L, (IX + 0)");
		output_code("LD", "H, (IX + 1)");
		output_code("DEC", "HL");
		output_code("LD", "(IX + 0), L");
		output_code("LD", "(IX + 1), H");
	    } else {  /* Postdecrement */
		output_code("LD", "L, (IX + 0)");
		output_code("LD", "H, (IX + 1)");
		output_code("LD", "E, L");
		output_code("LD", "D, H");
		output_code("DEC", "DE");
		output_code("LD", "(IX + 0), E");
		output_code("LD", "(IX + 1), D");
	    }
	    break;
	case CAST:
	    output_node(node->arg[0], break_label, continue_label);
	    /*
	     * Use get_decl_size() to do the right thing here. Not
	     * necessary right now.
	     */
	    break;
	case SIZEOF:
	    output_code("LD", "HL, %d", get_decl_size(node->arg[0]->decl));
	    break;
	case '*':
	    if (node->numargs == 1) {
                // Pointer dereference.
		output_node(node->arg[0], break_label, continue_label);
		output_code("PUSH", "HL");
		output_code("POP", "IX");
		if (!node->lhs) {
                    // Actually dereference it.
		    output_code("LD", "H, (IX + 1)");
		    output_code("LD", "L, (IX)");
		}
	    } else {
                // Multiplication.
		if (node->arg[0]->op == NUMBER ||
		    node->arg[1]->op == NUMBER) {

		    if (node->arg[0]->op == NUMBER) {
			n = node->arg[0];
			node->arg[0] = node->arg[1];
			node->arg[1] = n;
		    }

		    output_node(node->arg[0], break_label, continue_label);

		    imm = node->arg[1]->data.value;
		    if (imm < 0) {
			imm = -imm;
			output_code("LD", "A, 255");
			output_code("XOR", "H");
			output_code("XOR", "L");
			output_code("INC", "HL");
		    }

		    if (imm == 0) {
			output_code("LD", "HL, 0");
		    } else if ((imm & (imm - 1)) == 0) { /* Power of two */
			while (imm != 1) {
			    output_code("ADD", "HL, HL");
			    imm >>= 1;
			}
		    } else {
			if ((imm & 1) != 0) {
			    output_code("LD", "D, H");
			    output_code("LD", "E, L");
			    imm &= ~1;
			} else {
			    output_code("LD", "DE, 0");
			}
			state = 0;  /* state = 1 when HL contains the output */
			while (imm != 1) {
			    if ((imm & 1) != 0) {
				if (!state) {
				    output_code("EX", "DE, HL");
				}
				state = 1;
				output_code("ADD", "HL, DE");
			    }
			    if (state) {
				output_code("EX", "DE, HL");
				state = 0;
			    }
			    output_code("ADD", "HL, HL");
			    imm >>= 1;
			}
			/* Doesn't matter what "state" is */
			output_code("ADD", "HL, DE");
		    }
		} else {
		    output_comment("Unsupported operands: %s.",
			get_op_name(node->op));
		    unsupported++;
		}
	    }