Added a method to write variable sized unsigned values. Use this method for

all sorts of things in the object files. This does not only make the object
files smaller, but does also remove several limits (strings may be longer
than 255 bytes, several counters no longer have 8 or 16 bit limits).


git-svn-id: svn://svn.cc65.org/cc65/trunk@260 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2000-08-02 13:23:06 +00:00
parent 85d8b3badf
commit 097a01094e
29 changed files with 423 additions and 295 deletions

View File

@@ -6,10 +6,10 @@
/* */
/* */
/* */
/* (C) 1998 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
@@ -39,7 +39,7 @@
#include "check.h"
#include "symdefs.h"
#include "xmalloc.h"
/* ld65 */
#include "global.h"
#include "error.h"
@@ -69,14 +69,11 @@ static DbgSym* DbgSymPool [256];
static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O)
static DbgSym* NewDbgSym (unsigned char Type, ObjData* O)
/* Create a new DbgSym and return it */
{
/* Get the length of the symbol name */
unsigned Len = strlen (Name);
/* Allocate memory */
DbgSym* D = xmalloc (sizeof (DbgSym) + Len);
DbgSym* D = xmalloc (sizeof (DbgSym));
/* Initialize the fields */
D->Next = 0;
@@ -84,8 +81,7 @@ static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O)
D->Obj = O;
D->Expr = 0;
D->Type = Type;
memcpy (D->Name, Name, Len);
D->Name [Len] = '\0';
D->Name = 0;
/* Return the new entry */
return D;
@@ -143,17 +139,16 @@ DbgSym* ReadDbgSym (FILE* F, ObjData* O)
/* Read a debug symbol from a file, insert and return it */
{
unsigned char Type;
char Name [256];
DbgSym* D;
/* Read the type */
Type = Read8 (F);
/* Read the name */
ReadStr (F, Name);
/* Create a new debug symbol */
D = NewDbgSym (Type, O);
/* Create a new export */
D = NewDbgSym (Type, Name, O);
/* Read and assign the name */
D->Name = ReadStr (F);
/* Read the value */
if (Type & EXP_EXPR) {

View File

@@ -40,9 +40,11 @@
#include <stdio.h>
#include "../common/exprdefs.h"
#include "../common/filepos.h"
/* common */
#include "exprdefs.h"
#include "filepos.h"
/* ld65 */
#include "objdata.h"
@@ -62,7 +64,7 @@ struct DbgSym_ {
FilePos Pos; /* File position of definition */
ExprNode* Expr; /* Expression (0 if not def'd) */
unsigned char Type; /* Type of symbol */
char Name [1]; /* Name - dynamically allocated */
char* Name; /* Name - dynamically allocated */
};
@@ -90,3 +92,4 @@ void PrintDbgSymLabels (ObjData* O, FILE* F);

View File

@@ -42,7 +42,7 @@
#include "hashstr.h"
#include "symdefs.h"
#include "xmalloc.h"
/* ld65 */
#include "global.h"
#include "error.h"
@@ -175,7 +175,7 @@ Import* ReadImport (FILE* F, ObjData* Obj)
I = NewImport (Type, Obj);
/* Read the name */
I->V.Name = ReadMallocedStr (F);
I->V.Name = ReadStr (F);
/* Read the file position */
ReadFilePos (F, &I->Pos);
@@ -195,22 +195,23 @@ Import* ReadImport (FILE* F, ObjData* Obj)
static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
/* Create a new export and initialize it */
{
/* Get the length of the symbol name */
unsigned Len = strlen (Name);
/* Allocate memory */
Export* E = xmalloc (sizeof (Export) + Len);
Export* E = xmalloc (sizeof (Export));
/* Initialize the fields */
E->Next = 0;
E->Flags = 0;
E->Flags = 0;
E->Obj = Obj;
E->ImpCount = 0;
E->ImpList = 0;
E->Expr = 0;
E->Type = Type;
memcpy (E->Name, Name, Len);
E->Name [Len] = '\0';
if (Name) {
E->Name = xstrdup (Name);
} else {
/* Name will get added later */
E->Name = 0;
}
/* Return the new entry */
return E;
@@ -250,7 +251,7 @@ void InsertExport (Export* E)
if (Last) {
Last->Next = E;
} else {
HashTab [HashVal] = E;
HashTab [HashVal] = E;
}
ImpOpen -= E->ImpCount; /* Decrease open imports now */
xfree (L);
@@ -285,17 +286,16 @@ Export* ReadExport (FILE* F, ObjData* O)
/* Read an export from a file */
{
unsigned char Type;
char Name [256];
Export* E;
/* Read the type */
Type = Read8 (F);
/* Read the name */
ReadStr (F, Name);
/* Create a new export without a name */
E = NewExport (Type, 0, O);
/* Create a new export */
E = NewExport (Type, Name, O);
/* Read the name */
E->Name = ReadStr (F);
/* Read the value */
if (Type & EXP_EXPR) {

View File

@@ -6,10 +6,10 @@
/* */
/* */
/* */
/* (C) 1998 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
@@ -40,9 +40,11 @@
#include <stdio.h>
#include "../common/exprdefs.h"
#include "../common/filepos.h"
/* common */
#include "exprdefs.h"
#include "filepos.h"
/* ld65 */
#include "objdata.h"
#include "config.h"
@@ -80,7 +82,7 @@ struct Export_ {
FilePos Pos; /* File position of definition */
ExprNode* Expr; /* Expression (0 if not def'd) */
unsigned char Type; /* Type of export */
char Name [1]; /* Name - dynamically allocated */
char* Name; /* Name - dynamically allocated */
};

View File

@@ -35,8 +35,10 @@
#include <string.h>
#include "../common/xmalloc.h"
/* common */
#include "xmalloc.h"
/* ld65 */
#include "error.h"
#include "fileio.h"
@@ -117,14 +119,31 @@ void WriteVal (FILE* F, unsigned long Val, unsigned Size)
void WriteVar (FILE* F, unsigned long V)
/* Write a variable sized value to the file in special encoding */
{
/* We will write the value to the file in 7 bit chunks. If the 8th bit
* is clear, we're done, if it is set, another chunk follows. This will
* allow us to encode smaller values with less bytes, at the expense of
* needing 5 bytes if a 32 bit value is written to file.
*/
do {
unsigned char C = (V & 0x7F);
V >>= 7;
if (V) {
C |= 0x80;
}
Write8 (F, C);
} while (V != 0);
}
void WriteStr (FILE* F, const char* S)
/* Write a string to the file */
{
unsigned Len = strlen (S);
if (Len > 255) {
Internal ("String too long");
}
Write8 (F, (unsigned char) Len);
WriteVar (F, Len);
WriteData (F, S, Len);
}
@@ -210,37 +229,43 @@ long Read32Signed (FILE* F)
char* ReadStr (FILE* F, char* Str)
/* Read a string from the file. Str must hold 256 chars at max */
unsigned long ReadVar (FILE* F)
/* Read a variable size value from the file */
{
/* Read the length byte */
unsigned Len = Read8 (F);
/* The value was written to the file in 7 bit chunks LSB first. If there
* are more bytes, bit 8 is set, otherwise it is clear.
*/
unsigned char C;
unsigned long V = 0;
unsigned Shift = 0;
do {
/* Read one byte */
C = Read8 (F);
/* Encode it into the target value */
V |= ((unsigned long)(C & 0x7F)) << Shift;
/* Next value */
Shift += 7;
} while (C & 0x80);
/* Read the string itself */
ReadData (F, Str, Len);
/* Terminate the string and return it */
Str [Len] = '\0';
return Str;
/* Return the value read */
return V;
}
char* ReadMallocedStr (FILE* F)
/* Read a string from the file into a malloced area */
char* ReadStr (FILE* F)
/* Read a string from the file (the memory will be malloc'ed) */
{
/* Read the length byte */
unsigned Len = Read8 (F);
/* Read the length */
unsigned Len = ReadVar (F);
/* Allocate memory */
char* Str = xmalloc (Len + 1);
/* Read the string itself */
ReadData (F, Str, Len);
/* Allocate memory and read the string itself */
char* S = xmalloc (Len + 1);
ReadData (F, S, Len);
/* Terminate the string and return it */
Str [Len] = '\0';
return Str;
S [Len] = '\0';
return S;
}
@@ -248,10 +273,10 @@ char* ReadMallocedStr (FILE* F)
FilePos* ReadFilePos (FILE* F, FilePos* Pos)
/* Read a file position from the file */
{
/* The line number is encoded as 24 bit value to save some space */
Pos->Line = Read24 (F);
Pos->Col = Read8 (F);
Pos->Name = Read8 (F);
/* Read the data fields */
Pos->Line = ReadVar (F);
Pos->Col = ReadVar (F);
Pos->Name = ReadVar (F);
return Pos;
}
@@ -268,4 +293,4 @@ void* ReadData (FILE* F, void* Data, unsigned Size)

View File

@@ -40,7 +40,8 @@
#include <stdio.h>
#include "../common/filepos.h"
/* common */
#include "filepos.h"
@@ -65,6 +66,9 @@ void Write32 (FILE* F, unsigned long Val);
void WriteVal (FILE* F, unsigned long Val, unsigned Size);
/* Write a value of the given size to the output file */
void WriteVar (FILE* F, unsigned long V);
/* Write a variable sized value to the file in special encoding */
void WriteStr (FILE* F, const char* S);
/* Write a string to the file */
@@ -89,10 +93,10 @@ unsigned long Read32 (FILE* F);
long Read32Signed (FILE* F);
/* Read a 32 bit value from the file. Sign extend the value. */
char* ReadStr (FILE* F, char* Str);
/* Read a string from the file. Str must hold 256 chars at max */
unsigned long ReadVar (FILE* F);
/* Read a variable size value from the file */
char* ReadMallocedStr (FILE* F);
char* ReadStr (FILE* F);
/* Read a string from the file into a malloced area */
FilePos* ReadFilePos (FILE* F, FilePos* Pos);

View File

@@ -6,10 +6,10 @@
/* */
/* */
/* */
/* (C) 1998 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */
/* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
@@ -37,13 +37,15 @@
#include <string.h>
#include <errno.h>
#include "../common/exprdefs.h"
#include "../common/filepos.h"
#include "../common/libdefs.h"
#include "../common/objdefs.h"
#include "../common/symdefs.h"
#include "../common/xmalloc.h"
/* common */
#include "exprdefs.h"
#include "filepos.h"
#include "libdefs.h"
#include "objdefs.h"
#include "symdefs.h"
#include "xmalloc.h"
/* ld65 */
#include "error.h"
#include "exports.h"
#include "fileio.h"
@@ -54,7 +56,7 @@
/*****************************************************************************/
/* Data */
/* Data */
/*****************************************************************************/
@@ -111,7 +113,7 @@ static ObjData* ReadIndexEntry (void)
ObjData* O = NewObjData ();
/* Module name/flags/MTime/Start/Size */
O->Name = ReadMallocedStr (Lib);
O->Name = ReadStr (Lib);
O->Flags = Read16 (Lib);
Read32 (Lib); /* Skip MTime */
O->Start = Read32 (Lib);
@@ -119,7 +121,7 @@ static ObjData* ReadIndexEntry (void)
/* Skip the export size, then read the exports */
Read16 (Lib);
O->ExportCount = Read16 (Lib);
O->ExportCount = ReadVar (Lib);
O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
for (I = 0; I < O->ExportCount; ++I) {
O->Exports [I] = ReadExport (Lib, O);
@@ -127,7 +129,7 @@ static ObjData* ReadIndexEntry (void)
/* Skip the import size, then read the imports */
Read16 (Lib);
O->ImportCount = Read16 (Lib);
O->ImportCount = ReadVar (Lib);
O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
for (I = 0; I < O->ImportCount; ++I) {
O->Imports [I] = ReadImport (Lib, O);

View File

@@ -38,7 +38,8 @@
#include "../common/objdefs.h"
/* common */
#include "objdefs.h"
@@ -104,3 +105,4 @@ void FreeObjData (ObjData* O);

View File

@@ -40,7 +40,7 @@
/* common */
#include "xmalloc.h"
/* ld65 */
#include "dbgsyms.h"
#include "error.h"
@@ -107,14 +107,14 @@ void ObjReadFiles (FILE* F, ObjData* O)
{
unsigned I;
O->FileCount = Read16 (F);
O->FileCount = ReadVar (F);
O->Files = xmalloc (O->FileCount * sizeof (char*));
for (I = 0; I < O->FileCount; ++I) {
/* Skip MTime and size */
Read32 (F);
Read32 (F);
/* Read the filename */
O->Files [I] = ReadMallocedStr (F);
O->Files [I] = ReadStr (F);
}
}
@@ -125,7 +125,7 @@ void ObjReadImports (FILE* F, ObjData* O)
{
unsigned I;
O->ImportCount = Read16 (F);
O->ImportCount = ReadVar (F);
O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
for (I = 0; I < O->ImportCount; ++I) {
O->Imports [I] = ReadImport (F, O);
@@ -140,7 +140,7 @@ void ObjReadExports (FILE* F, ObjData* O)
{
unsigned I;
O->ExportCount = Read16 (F);
O->ExportCount = ReadVar (F);
O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
for (I = 0; I < O->ExportCount; ++I) {
O->Exports [I] = ReadExport (F, O);
@@ -155,7 +155,7 @@ void ObjReadDbgSyms (FILE* F, ObjData* O)
{
unsigned I;
O->DbgSymCount = Read16 (F);
O->DbgSymCount = ReadVar (F);
O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
for (I = 0; I < O->DbgSymCount; ++I) {
O->DbgSyms [I] = ReadDbgSym (F, O);
@@ -169,7 +169,7 @@ void ObjReadSections (FILE* F, ObjData* O)
{
unsigned I;
O->SectionCount = Read8 (F);
O->SectionCount = ReadVar (F);
O->Sections = xmalloc (O->SectionCount * sizeof (Section*));
for (I = 0; I < O->SectionCount; ++I) {
O->Sections [I] = ReadSection (F, O);
@@ -226,3 +226,4 @@ void ObjAdd (FILE* Obj, const char* Name)

View File

@@ -215,7 +215,7 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Read a section from a file */
{
unsigned HashVal;
char Name [256];
char* Name;
unsigned long Size;
unsigned char Align;
unsigned char Type;
@@ -223,7 +223,7 @@ Section* ReadSection (FILE* F, ObjData* O)
Section* Sec;
/* Read the name */
ReadStr (F, Name);
Name = ReadStr (F);
/* Read the size */
Size = Read32 (F);
@@ -237,7 +237,7 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Print some data */
if (Verbose > 1) {
printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
O->Name, Name, Size, Align, Type);
O->Name, Name, Size, Align, Type);
}
/* Create a hash over the name and try to locate the segment in the table */
@@ -254,6 +254,9 @@ Section* ReadSection (FILE* F, ObjData* O)
HashTab [HashVal] = S;
}
/* We have the segment and don't need the name any longer */
xfree (Name);
/* Allocate the section we will return later */
Sec = NewSection (S, Align, Type);
@@ -281,20 +284,8 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Handle the different fragment types */
switch (Type) {
case FRAG_LITERAL8:
Frag = NewFragment (FRAG_LITERAL, Read8 (F), Sec);
break;
case FRAG_LITERAL16:
Frag = NewFragment (FRAG_LITERAL, Read16 (F), Sec);
break;
case FRAG_LITERAL24:
Frag = NewFragment (FRAG_LITERAL, Read24 (F), Sec);
break;
case FRAG_LITERAL32:
Frag = NewFragment (FRAG_LITERAL, Read32 (F), Sec);
case FRAG_LITERAL:
Frag = NewFragment (Type, ReadVar (F), Sec);
break;
case FRAG_EXPR8:
@@ -310,7 +301,7 @@ Section* ReadSection (FILE* F, ObjData* O)
case FRAG_FILL:
/* Will allocate memory, but we don't care... */
Frag = NewFragment (FRAG_FILL, Read16 (F), Sec);
Frag = NewFragment (Type, ReadVar (F), Sec);
break;
default: