📄 pass3.c
字号:
/****************************************************************************** * FREXX PROGRAMMING LANGUAGE * ****************************************************************************** Pass3.c Compiling functions *****************************************************************************//************************************************************************ * * * fpl.library - A shared library interpreting script langauge. * * Copyright (C) 1992-1995 FrexxWare * * Author: Daniel Stenberg * * * * This program is free software; you may redistribute for non * * commercial purposes only. Commercial programs must have a written * * permission from the author to use FPL. FPL is *NOT* public domain! * * Any provided source code is only for reference and for assurance * * that users should be able to compile FPL on any operating system * * he/she wants to use it in! * * * * You may not change, resource, patch files or in any way reverse * * engineer anything in the FPL package. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * * * Daniel Stenberg * * Ankdammsgatan 36, 4tr * * S-171 43 Solna * * Sweden * * * * FidoNet 2:201/328 email:dast@sth.frontec.se * * * ************************************************************************/#ifdef AMIGA#include <exec/types.h>#include <proto/exec.h>#include <libraries/dos.h>#include <proto/dos.h>#include <exec/libraries.h>#include <dos.h>#elif defined(UNIX)#include <sys/types.h>#include <sys/stat.h>#endif#include <stdlib.h>#include <stdio.h>#include <string.h>#include <stddef.h>#include "script.h"#include "pass2.h"#include "pass3.h"extern struct Pass2Data pass2_first_data;struct Pass2Data pass3_first_data;struct Pass2Data *pass3_last_data=&pass3_first_data;struct Pass2Data pass3_current_data;struct Pass3String pass3_first_string;struct Pass3String *pass3_last_string=&pass3_first_string;long final_offset_count=0;long string_offset_count=0;long *var_global_number;long *var_local_number;struct Pass3LabelRef *label_list;struct Pass3LabelRef *continue_list;struct Pass3LabelRef *break_list;struct Pass3LabelRef *function_list;extern long labelcount; /* Number of labels (from pass2) */extern long total_number_of_variables; /* Number of variables (from pass2) */extern long total_number_of_functions;extern long max_level; /* Number of levels (from pass2) */char link_flags=0;#ifdef DEBUG_OUTPUTstatic void Describe(struct Data *scr, struct Pass2Data *data){ char *s[]={ "NOTHING", "DECLARE", "REF_LOCAL_SYMBOL", "REF_GLOBAL_SYMBOL", "REF_EXPORT_SYMBOL", "POSTINC", "POSTDEC", "PREINC", "PREDEC", "CONTENTSOF", "NUM_CONSTANT", "STRING_CONSTANT", "OPEN_PAREN", "CLOSE_PAREN", "ONCECOMPLEMENT", "NOTOPERATOR", "NEGATE", "COMMA", "NOTEQUAL", "GREATER", "SHIFTRIGHT", "GREATEQ", "LESS", "SHIFTLEFT", "LESSEQ", "XOR", "REMAIN", "DIVISION", "MULTIPLY", "CONDOPEND", "CONDOPSTART", "MINUS", "PLUS", "BINARYOR", "BINARYAND", "LOGICOR", "LOGICAND", "EQUAL", "STRING_APPEND", "END_OF_EXPR", "ASSIGN_LOCAL_SYMBOL", /* [var num] [assign type] */ "ASSIGN_GLOBAL_SYMBOL", /* [var num] [assign type] */ "ASSIGN_EXPORT_SYMBOL", /* [hash num] [assign type] [name] */ "LABEL_GOTO", /* [number] */ "IFNOT_BRANCH", /* if branch */ "IF_BRANCH", /* if branch */ "CALL_LOCAL_FUNCTION", "CALL_EXPORT_FUNCTION", "CALL_INTERNAL_FUNCTION", "SWITCH", "CASE", /* [label number] */ "BREAK_EXPR", /* Followed by an expression and several GOTO_BREAK and END_OF_EXPR */ "RETURN", "RESIZE", "OPEN_BRACKET", "CLOSE_BRACKET", "ASSIGN_ARGUMENT", /* [var number], [arg number] */ "EXPORT_FUNCTION", /* [type], [number], [offset] */ "RESET_VARIABLE", /* [number] */ "OPEN_BRACE", "CLOSE_BRACE", "EXIT", /* expression */ "VARIABLE_REFERENCE", "TYPE_OF_ARGUMENTS", /* [string] */ "JUMP_OVER_ELSE", /* jump over olse */ "MAIN_START", "MAIN_END", "IFNOT_BRANCH_BREAK",/* if not branch to break */ "IFNOT_BRANCH_ELSE",/* if not branch to else */ "LOCAL_FUNCTION", /* [type], [number], [offset] */ "LABEL", "LABEL_BEGIN", /* [number] */ "LABEL_CONTINUE", /* [level] */ "LABEL_BREAK", /* [level] */ "GOTO_BEGIN", /* [number] */ "GOTO_BREAK", /* [level] */ "LABEL_ELSE", /* else [number]*/ "END_OF_ELSE", /* else [number]*/ "FUNCTION_START", /* [function number] */ "CONTINUE", /* level */ "AMOUNT_VARIABLES", "END_OF_FUNCTION", "LINE_NUMBER", }; if(data->code>sizeof(s)/sizeof(char *)) printf("UNKNOWN\n"); else { printf("%04x %2d [%02x] %-20s", data->final_offset, data->final_size, data->code, s[data->code]); if (data->flags&P2D_FLAG_NUM0) printf(" [%08x]", data->num[0]); if (data->flags&P2D_FLAG_NUM1) printf(" [%08x]", data->num[1]); if (data->flags&P2D_FLAG_NUM2) printf(" [%08x]", data->num[2]); if (data->flags&P2D_FLAG_STRING) { if (data->pass3_string_offset[0]) printf(" [%08x]", data->pass3_string_offset[0]); } if (data->flags&P2D_FLAG_STRING2) { if (data->pass3_string_offset[1]) printf(" [%08x]", data->pass3_string_offset[1]); } if (data->flags&P2D_FLAG_STRING) printf(" (%.*s) (%04x) (%d)", data->strlen[0], data->string[0], data->pass3_string_offset[0], data->strlen[0]); if (data->flags&P2D_FLAG_STRING2) printf(" (%.*s) (%04x) (%d)", data->strlen[1], data->string[1], data->pass3_string_offset[1], data->strlen[1]); printf("\n"); }}#endifReturnCode Pass3Open(struct Data *scr, char *file){#ifdef DEBUG_OUTPUT if (scr->cmdline[LINE_VERBOSE]&VERBOSE_PASS3) printf("\n-------------------------------\nPASS3 START\n-------------------------------\n");#endif max_level+=2; /* for safety */ GETMEM(label_list, sizeof(struct Pass3LabelRef)*(labelcount+1)); memset(label_list, 0, sizeof(struct Pass3LabelRef)*(labelcount+1)); GETMEM(continue_list, sizeof(struct Pass3LabelRef)*(max_level+1)); memset(continue_list, 0, sizeof(struct Pass3LabelRef)*(max_level+1)); GETMEM(break_list, sizeof(struct Pass3LabelRef)*(max_level+1)); memset(break_list, 0, sizeof(struct Pass3LabelRef)*(max_level+1)); GETMEM(function_list, sizeof(struct Pass3LabelRef)*(total_number_of_functions+1)); memset(function_list, 0, sizeof(struct Pass3LabelRef)*(total_number_of_functions+1)); GETMEM(var_global_number, sizeof(long)*(total_number_of_variables+1)); memset(var_global_number, 0, sizeof(long)*(total_number_of_variables+1)); GETMEM(var_local_number, sizeof(long)*(total_number_of_variables+1)); memset(var_local_number, 0, sizeof(long)*(total_number_of_variables+1)); memset(&pass3_first_data, 0, sizeof(struct Pass2Data)); pass3_last_data=&pass3_first_data; memset(&pass3_current_data, 0, sizeof(struct Pass2Data)); memset(&pass3_first_string, 0, sizeof(struct Pass3String)); pass3_last_string=&pass3_first_string; final_offset_count=0; string_offset_count=0; return FPL_OK;}void Pass3CheckSystem(){ long test=1;/* if (check commandline option) */ link_flags |= FLAG_WORD_ALIGNED; if (*(char *)&test==1) link_flags |= FLAG_LOW_BYTE_FIRST;}ReturnCode Pass3PushPut(struct Data *scr, struct Pass2Data *pass2_data){ struct Pass2Data *new; { long size=sizeof(struct Pass2Data); if (!pass2_data->flags) size=offsetof(struct Pass2Data, num); /* OBS: The structure is allocated shorter now */ GETMEM(new, size); memcpy(new, pass2_data, size); } pass3_last_data->next=new; new->prev=pass3_last_data; pass3_last_data=new; new->final_offset=final_offset_count; final_offset_count+=new->final_size*2;#ifdef DEBUG_OUTPUT if (scr->cmdline[LINE_VERBOSE]&VERBOSE_PASS3) { Describe(scr, new); }#endif return FPL_OK;}ReturnCode Pass3PushLineNumber(struct Data *scr, long line){ struct Pass2Data *new; GETMEM(new, sizeof(struct Pass2Data)); memset(new, 0, sizeof(struct Pass2Data)); new->code=PASS2_LINE_NUMBER; new->line=line; new->num[0]=line; new->flags|=P2D_FLAG_NUM0; new->final_size=3; pass3_last_data->next=new; new->prev=pass3_last_data; pass3_last_data=new; new->final_offset=final_offset_count; final_offset_count+=new->final_size*2;#ifdef DEBUG_OUTPUT if (scr->cmdline[LINE_VERBOSE]&VERBOSE_PASS3) { Describe(scr, new); }#endif return FPL_OK;}void write_num(char *dest, long num, long size){ long count; if (link_flags&FLAG_LOW_BYTE_FIRST) { for (count=0; count<size; count++) { dest[count]=num&255; num>>=8; } } else { for (count=size-1; count>=0; count--) { dest[count]=num&255; num>>=8; } }}void PutNumHunk(char *hunk_name, long num, FILE *fp){ char tempbuffer[sizeof(long)]; fwrite(hunk_name, 1, 4, fp); write_num(tempbuffer, sizeof(long), sizeof(long)); /* Put size of hunk */ fwrite(tempbuffer, 1, sizeof(long), fp); write_num(tempbuffer, num, sizeof(long)); /* Put data of hunk */ fwrite(tempbuffer, 1, sizeof(long), fp);}void PutInteger(long num, FILE *fp){ char tempbuffer[sizeof(long)]; write_num(tempbuffer, num, sizeof(long)); /* Put data of hunk */ fwrite(tempbuffer, 1, sizeof(long), fp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -