⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pass3.c

📁 类PASCAL语言的编译器,LINUX环境的,我没试过是否正确.
💻 C
📖 第 1 页 / 共 3 页
字号:
}void PutStringHunk(char *hunk_name, char *string, FILE *fp){  char tempbuffer[sizeof(long)];  long len=strlen(string);  len+=3-((len+1)%3);  fwrite(hunk_name, 1, 4, fp);  write_num(tempbuffer, len, sizeof(long)); /* Put size of hunk */  fwrite(tempbuffer, 1, sizeof(long), fp);  fwrite(string, 1, len, fp);}ReturnCode Pass3Close(struct Data *scr, char *file, char *origfile){  struct Pass2Data *count;  FILE *fp;  char *dest;  char *destcount;  char *filename_buffer;  final_offset_count+=sizeof(long); /* append size of length */  GETMEM(dest, final_offset_count);  destcount=dest;  count=&pass3_first_data;#ifdef DEBUG_OUTPUT  if (scr->cmdline[LINE_VERBOSE]&VERBOSE_FINAL)    printf("\nRESULT PASS3\n");#endif  while (count=count->next) {    if (count->final_size) { /* ignore if no size */      long size=sizeof(short);#ifdef DEBUG_OUTPUT      if (scr->cmdline[LINE_VERBOSE]&VERBOSE_FINAL) {        Describe(scr, count);      }#endif      write_num(destcount, (long)count->code, sizeof(short));      destcount+=sizeof(short);      if (count->flags) {        /* If no 'flags' then the structure is shorter and can't be read */        if (count->flags&P2D_FLAG_NUM0) {          write_num(destcount, count->num[0], sizeof(long));          destcount+=sizeof(long);          size+=sizeof(long);        }        if (count->flags&P2D_FLAG_NUM1) {          write_num(destcount, count->num[1], sizeof(long));          destcount+=sizeof(long);          size+=sizeof(long);        }        if (count->flags&P2D_FLAG_NUM2) {          write_num(destcount, count->num[2], sizeof(long));          destcount+=sizeof(long);          size+=sizeof(long);        }        if (count->pass3_string_offset[0]) {          write_num(destcount, count->pass3_string_offset[0], sizeof(long));          destcount+=sizeof(long);          size+=sizeof(long);        }        if (count->pass3_string_offset[1]) {          write_num(destcount, count->pass3_string_offset[1], sizeof(long));          destcount+=sizeof(long);          size+=sizeof(long);        }      }      if (size!=count->final_size*2)        printf("ERROR\n");      if (destcount>dest+final_offset_count) {        printf("Overflow error!\n");        exit(0);      }    }  }  {    struct Pass3String *count;    count=&pass3_first_string;    while (count=count->next) {      if (count->strlen<0) {        write_num(destcount, count->hash, sizeof(long));      } else {        write_num(destcount, count->strlen, sizeof(long));      }      destcount+=sizeof(long);      memcpy(destcount, count->string, count->total_length);      destcount+=count->total_length;#ifdef DEBUG_OUTPUT      if (scr->cmdline[LINE_VERBOSE]&VERBOSE_FINAL) {        printf("%04x (%d) \"%.*s\"\n", count->offset, count->strlen, count->strlen, count->string);      }#endif      if (destcount>dest+final_offset_count) {        printf("Overflow2 error!\n");        exit(0);      }    }  }  {    long len = strlen(file);    if(#ifdef AMIGA       (':' == file[len-1]) ||#endif       ('/' == file[len-1]) ) {      long len2=strlen(origfile)-1;      while(len2>=0) {        if('/' == origfile[len2]#ifdef AMIGA           || ':' == origfile[len2]#endif           ) {          origfile = &origfile[++len2];          break;        }        --len2;      }      GETMEM(filename_buffer, len+strlen(origfile)+5);      strcpy(filename_buffer, file);      strcat(filename_buffer, origfile);      file = filename_buffer;    }    else {     GETMEM(filename_buffer, len+5);     strcpy(filename_buffer, file);    }  }  {    long stringlen=strlen(file);    if (stringlen >3) {      if(!stricmp(&filename_buffer[stringlen-4], ".fpl")) {        if (isupper(filename_buffer[stringlen-1]))          filename_buffer[stringlen-1]='C';        else          filename_buffer[stringlen-1]='c';      }      else if (stricmp(&filename_buffer[stringlen-4], ".fpc")) {        strcat(filename_buffer, ".FPC");      }    }  }		/* Open file, and write hunks */  fp=fopen(filename_buffer, "w");  if (!fp)    return FPLERR_OPEN_ERROR;  fwrite(HUNK_HEADER, 1, strlen(HUNK_HEADER), fp);  fwrite(&link_flags, 1, 1, fp);  PutNumHunk(HUNK_VERSION, VERSION_NUM, fp);  PutNumHunk(HUNK_REQUIRE_FPL, REQUIRE_FPL, fp);  PutStringHunk(HUNK_FILE, origfile, fp);  fwrite(HUNK_CODE, 1, strlen(HUNK_CODE), fp);  PutInteger(final_offset_count, fp); /* Put size of hunk */  fwrite(dest, 1, final_offset_count, fp);  /*    Construct symbol hunk  */  {    struct Pass3String *count;    long size=0;    count=&pass3_first_string;    while (count=count->next) {      if (count->type & (STRING_IS_FUNCTION|STRING_IS_VARIABLE))        size+=sizeof(long)*2;    }    destcount=dest;    count=&pass3_first_string;    while (count=count->next) {      if (count->type & (STRING_IS_FUNCTION|STRING_IS_VARIABLE)) {        write_num(destcount, count->type, sizeof(long)); /* Put type */        destcount+=sizeof(long);        write_num(destcount, count->offset, sizeof(long)); /* Put offset */        destcount+=sizeof(long);#ifdef DEBUG_OUTPUT      if (scr->cmdline[LINE_VERBOSE]&VERBOSE_SYMBOL) {        if (count->type & STRING_IS_DECLARED)          printf("Declare ");        else          printf("Reference ");        if (count->type & STRING_IS_FUNCTION)          printf("function");        else          printf("variable");        printf(":%.*s\n", count->strlen, count->string);      }#endif      }    }    fwrite(HUNK_SYMBOLS, 1, strlen(HUNK_SYMBOLS), fp);    PutInteger(destcount-dest, fp);  /* Put size of hunk */    fwrite(dest, 1, destcount-dest, fp);  }  fclose(fp);  return FPL_OK;}ReturnCode Pass3Put(struct Data *scr, Pass2 code, struct Pass2Data *pass2_data,                    char size){  ReturnCode ret;  pass3_current_data.code=code;  pass3_current_data.final_size=size;  if (pass3_current_data.code!=PASS2_NOTHING) {    CALL(Pass3PushPut(scr, &pass3_current_data));  }  memset(&pass3_current_data, 0, sizeof(pass3_current_data));  pass3_current_data.code=PASS2_NOTHING;  pass3_current_data.level=0;  pass3_current_data.line=0;  pass3_current_data.row=0;  pass3_current_data.next=NULL;  return FPL_OK;}ReturnCode Pass3PutArg(struct Data *scr, long arg, struct Pass2Data *pass2_data){  if (!(pass2_data->flags&P2D_FLAG_NUM0)) {    pass2_data->num[0]=arg;    pass2_data->flags|=P2D_FLAG_NUM0;  } else if (!(pass2_data->flags&P2D_FLAG_NUM1)) {    pass2_data->num[1]=arg;    pass2_data->flags|=P2D_FLAG_NUM1;  } else if (!(pass2_data->flags&P2D_FLAG_NUM2)) {    pass2_data->num[2]=arg;    pass2_data->flags|=P2D_FLAG_NUM2;  }  return FPL_OK;}ReturnCode Pass3PutLabelRef(struct Data *scr, struct Pass3LabelRef *label_list,                            long number, long *address){  struct Pass3LabelRef *new;  GETMEM(new, sizeof(struct Pass3LabelRef));  memset(new, 0, sizeof(struct Pass3LabelRef));  new->label_store=address;  new->next=label_list[number].next;  label_list[number].next=new;  return FPL_OK;}long Pass3FindLocalIdentifier(char *name){  struct Pass2Data *count=pass2_first_data.next;  while (count) {    if (count->flags&P2D_FLAG_STRING) {      if (count->code==PASS2_FUNCTION_START) { /* Function */        if (!(count->num[0]&FPL_EXPORT_SYMBOL)) {          if (!strcmp(count->string[0], name))            return count->num[0];        }      }      if (count->code==PASS2_DECLARE) { /* Variable */        if ((count->num[0]&FPL_GLOBAL_SYMBOL) &&            !(count->num[0]&FPL_EXPORT_SYMBOL)) {          if (!strcmp(count->string[0], name))            return var_global_number[count->num[1]];        }      }    }    count=count->next;  }  return -1;}ReturnCode Pass3ExtractLabelRef(struct Data *scr, struct Pass3LabelRef *label_list,                                long number){  struct Pass3LabelRef *count;  struct Pass3LabelRef *next;  count=label_list[number].next;  if (count) {    if (!label_list[number].label_offset) {      printf("Label error: %ld\n", number);      return FPLERR_IDENTIFIER_NOT_FOUND;    }    while (count) {      *(count->label_store)=label_list[number].label_offset;      next=count->next;      FREE(count);      count=next;    }  }  label_list[number].label_offset=0;  label_list[number].next=NULL;  return FPL_OK;}ReturnCode Pass3PutString(struct Data *scr, struct Pass2Data *pass2_data,                          long hash, char ishash, short num, char type){  ReturnCode ret;  struct Pass3String *new=NULL;  struct Pass3String *count;  count=pass3_first_string.next;  while (count) {    if (ishash) {      if (hash==count->hash &&          !strcmp(pass2_data->string[num], count->string)) {        new=count;        break;      }    } else {      if (pass2_data->strlen[num]==count->strlen &&          !memcmp(pass2_data->string[num], count->string, count->strlen)) {        new=count;        break;      }    }    count=count->next;  }  if (!new) {    pass2_data->pass3_string_offset[num]=string_offset_count;    GETMEM(new, sizeof(struct Pass3String));    memset(new, 0, sizeof(struct Pass3String));      new->offset=string_offset_count;    new->total_length=pass2_data->strlen[num]+1;    if (link_flags&FLAG_WORD_ALIGNED) {      new->total_length=(new->total_length+1)&~1;    }    string_offset_count+=new->total_length;    new->string=pass2_data->string[num];    new->type=type;    if (ishash) {      new->strlen=-1;      new->hash=hash;    } else      new->strlen=pass2_data->strlen[num];    pass3_last_string->next=new;    pass3_last_string=new;  } else {    new->type|=type;    pass2_data->pass3_string_offset[num]=new->offset;  }  CALL(Pass3PutLabelRef(scr, &new->references, 0, &pass2_data->pass3_string_offset[num]));  return FPL_OK;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -