?? atom.c
字號:
/****************************************************************************\
Copyright (c) 2002, NVIDIA Corporation.
NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms. If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.
THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.
IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
//
// atom.c
//
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "slglobals.h"
#undef malloc
#undef realloc
#undef free
///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// String table: //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
static const struct {
int val;
const char *str;
} tokens[] = {
AND_SY, "&&",
ASSIGNMINUS_SY, "-=",
ASSIGNMOD_SY, "%=",
ASSIGNPLUS_SY, "+=",
ASSIGNSLASH_SY, "/=",
ASSIGNSTAR_SY, "*=",
ASM_SY, "asm",
BOOLEAN_SY, "bool",
BREAK_SY, "break",
CASE_SY, "case",
COLONCOLON_SY, "::",
CONST_SY, "const",
CONTINUE_SY, "continue",
DEFAULT_SY, "default",
DISCARD_SY, "discard",
DO_SY, "do",
EQ_SY, "==",
ELSE_SY, "else",
EXTERN_SY, "extern",
FLOAT_SY, "float",
FLOATCONST_SY, "<float-const>",
FLOATHCONST_SY, "<floath-const>",
FLOATXCONST_SY, "<floatx-const>",
FOR_SY, "for",
GE_SY, ">=",
GG_SY, ">>",
GOTO_SY, "goto",
IDENT_SY, "<ident>",
IF_SY, "if",
IN_SY, "in",
INLINE_SY, "inline",
INOUT_SY, "inout",
INT_SY, "int",
INTCONST_SY, "<int-const>",
INTERNAL_SY, "__internal",
LE_SY, "<=",
LL_SY, "<<",
MINUSMINUS_SY, "--",
NE_SY, "!=",
OR_SY, "||",
OUT_SY, "out",
PACKED_SY, "__packed",
PACKED_SY, "packed",
PLUSPLUS_SY, "++",
RETURN_SY, "return",
STATIC_SY, "static",
STRUCT_SY, "struct",
STRCONST_SY, "<string-const>",
SWITCH_SY, "switch",
THIS_SY, "this",
TYPEDEF_SY, "typedef",
TYPEIDENT_SY, "<type-ident>",
UNIFORM_SY, "uniform",
VOID_SY, "void",
WHILE_SY, "while",
};
///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////// String table: //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
#define INIT_STRING_TABLE_SIZE 16384
typedef struct StringTable_Rec {
char *strings;
int nextFree;
int size;
} StringTable;
/*
* InitStringTable() - Initialize the string table.
*
*/
static int InitStringTable(StringTable *stable)
{
stable->strings = (char *) malloc(INIT_STRING_TABLE_SIZE);
if (!stable->strings)
return 0;
// Zero-th offset means "empty" so don't use it.
stable->nextFree = 1;
stable->size = INIT_STRING_TABLE_SIZE;
return 1;
} // InitStringTable
/*
* FreeStringTable() - Free the string table.
*
*/
static void FreeStringTable(StringTable *stable)
{
if (stable->strings)
free(stable->strings);
stable->strings = NULL;
stable->nextFree = 0;
stable->size = 0;
} // FreeStringTable
/*
* HashString() - Hash a string with the base hash function.
*
*/
static int HashString(const char *s)
{
int hval = 0;
while (*s) {
hval = (hval*13507 + *s*197) ^ (hval >> 2);
s++;
}
return hval & 0x7fffffff;
} // HashString
/*
* HashString2() - Hash a string with the incrimenting hash function.
*
*/
static int HashString2(const char *s)
{
int hval = 0;
while (*s) {
hval = (hval*729 + *s*37) ^ (hval >> 1);
s++;
}
return hval;
} // HashString2
/*
* AddString() - Add a string to a string table. Return it's offset.
*
*/
static int AddString(StringTable *stable, const char *s)
{
int len, loc;
char *str;
len = strlen(s);
if (stable->nextFree + len + 1 >= stable->size) {
assert(stable->size < 1000000);
str = (char *) malloc(stable->size*2);
memcpy(str, stable->strings, stable->size);
free(stable->strings);
stable->strings = str;
}
loc = stable->nextFree;
strcpy(&stable->strings[loc], s);
stable->nextFree += len + 1;
return loc;
} // AddString
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// Hash table: ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
#define INIT_HASH_TABLE_SIZE 2047
#define HASH_TABLE_MAX_COLLISIONS 3
typedef struct HashEntry_Rec {
int index; // String table offset of string representation
int value; // Atom (symbol) value
} HashEntry;
typedef struct HashTable_Rec {
HashEntry *entry;
int size;
int entries;
int counts[HASH_TABLE_MAX_COLLISIONS + 1];
} HashTable;
/*
* InitHashTable() - Initialize the hash table.
*
*/
static int InitHashTable(HashTable *htable, int fsize)
{
int ii;
htable->entry = (HashEntry *) malloc(sizeof(HashEntry)*fsize);
if (!htable->entry)
return 0;
htable->size = fsize;
for (ii = 0; ii < fsize; ii++) {
htable->entry[ii].index = 0;
htable->entry[ii].value = 0;
}
htable->entries = 0;
for (ii = 0; ii <= HASH_TABLE_MAX_COLLISIONS; ii++)
htable->counts[ii] = 0;
return 1;
} // InitHashTable
/*
* FreeHashTable() - Free the hash table.
*
*/
static void FreeHashTable(HashTable *htable)
{
if (htable->entry)
free(htable->entry);
htable->entry = NULL;
htable->size = 0;
htable->entries = 0;
} // FreeHashTable
/*
* Empty() - See if a hash table entry is empty.
*
*/
static int Empty(HashTable *htable, int hashloc)
{
assert(hashloc >= 0 && hashloc < htable->size);
if (htable->entry[hashloc].index == 0) {
return 1;
} else {
return 0;
}
} // Empty
/*
* Match() - See if a hash table entry is matches a string.
*
*/
static int Match(HashTable *htable, StringTable *stable, const char *s, int hashloc)
{
int strloc;
strloc = htable->entry[hashloc].index;
if (!strcmp(s, &stable->strings[strloc])) {
return 1;
} else {
return 0;
}
} // Match
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// Atom table: ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
#define INIT_ATOM_TABLE_SIZE 1024
struct AtomTable_Rec {
StringTable stable; // String table.
HashTable htable; // Hashes string to atom number and token value. Multiple strings can
// have the same token value but each unique string is a unique atom.
int *amap; // Maps atom value to offset in string table. Atoms all map to unique
// strings except for some undefined values in the lower, fixed part
// of the atom table that map to "<undefined>". The lowest 256 atoms
// correspond to single character ASCII values except for alphanumeric
// characters and '_', which can be other tokens. Next come the
// language tokens with their atom values equal to the token value.
// Then come predefined atoms, followed by user specified identifiers.
int *arev; // Reversed atom for symbol table use.
int nextFree;
int size;
};
static AtomTable latable = { 0 };
AtomTable *atable = &latable;
static int AddAtomFixed(AtomTable *atable, const char *s, int atom);
/*
* GrowAtomTable() - Grow the atom table to at least "size" if it's smaller.
*
*/
static int GrowAtomTable(AtomTable *atable, int size)
{
int *newmap, *newrev;
if (atable->size < size) {
if (atable->amap) {
newmap = realloc(atable->amap, sizeof(int)*size);
newrev = realloc(atable->arev, sizeof(int)*size);
} else {
newmap = malloc(sizeof(int)*size);
newrev = malloc(sizeof(int)*size);
atable->size = 0;
}
if (!newmap || !newrev) {
/* failed to grow -- error */
if (newmap)
atable->amap = newmap;
if (newrev)
atable->amap = newrev;
return -1;
}
memset(&newmap[atable->size], 0, (size - atable->size) * sizeof(int));
memset(&newrev[atable->size], 0, (size - atable->size) * sizeof(int));
atable->amap = newmap;
atable->arev = newrev;
atable->size = size;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -