Better ANSI C compatibility and portability:

+ Addition of function prototypes and include files.
+ Change function definitions to ANSI C style.
+ Initial support for CMake
This commit is contained in:
carl 2019-02-19 00:44:39 +08:00
parent cc27fd471d
commit d825e962ed
9 changed files with 491 additions and 387 deletions

View file

@ -0,0 +1,28 @@
cmake_minimum_required (VERSION 3.2)
project(em_topgen)
set(SRC
main.c
topgen.c
Lpars.c
LLlex.c
symtab.c
symtab.h
pattern.c
hash.c
token.h
tunable.h
)
set(INCLUDE_DIRS
.
${CMAKE_CURRENT_BINARY_DIR}
)
add_custom_command(
OUTPUT topgen.c Lpars.c Lpars.h
COMMAND ${CMAKE_COMMAND} -E env LLGEN_LIB_DIR=${CMAKE_CURRENT_SOURCE_DIR}/../LLgen/lib ${CMAKE_CURRENT_BINARY_DIR}/../LLgen/LLgen "${CMAKE_CURRENT_SOURCE_DIR}/topgen.g"
# COMMAND ${CMAKE_CURRENT_BINARY_DIR}/../LLgen/llgen "${CMAKE_CURRENT_SOURCE_DIR}/topgen.g"
MAIN_DEPENDENCY topgen.g
DEPENDS LLgen)
add_executable(topgen ${SRC})
target_include_directories(topgen PRIVATE ${INCLUDE_DIRS})
target_link_libraries(topgen)
install(TARGETS topgen DESTINATION bin)

View file

@ -21,8 +21,11 @@ static struct token aside; /* to put currrent token aside, when a token
int newline, lineno; /* To keep track of linenumbers */
extern FILE *input; /* file descriptor of machine table */
LLlex() {
register c;
extern void error(char *s, char* s1);
int LLlex(void) {
register int c;
if (aside.t_tokno) { /* A token was pushed aside, return it now */
dot = aside;
@ -52,7 +55,7 @@ LLlex() {
if (c == EOF) {
c = lineno;
lineno = l;
error("Unterminated comment");
error("Unterminated comment", "");
lineno = c;
c = EOF;
}
@ -116,13 +119,14 @@ LLlex() {
}
}
LLmessage(d) {
void LLmessage(int d)
{
static int savlineno;
if (savlineno != lineno) {
/* Only an error message if on another line number */
savlineno = lineno;
error("Syntax error");
error("Syntax error","");
}
if (d > 0) {
/* "d" is the token to be inserted.

View file

@ -13,78 +13,84 @@
#include <stdio.h>
#include <string.h>
#include "misc.h"
#include "hash.h"
struct hlist { /* linear list of pattern numbers */
int h_patno;
struct hlist *h_next;
struct hlist
{ /* linear list of pattern numbers */
int h_patno;
struct hlist *h_next;
};
static struct hlist *hashtable[129]; /* an array of ptr's to these lists,
* the index in the array is the
* result of hashing
*/
static struct hlist *hashtable[129]; /* an array of ptr's to these lists,
* the index in the array is the
* result of hashing
*/
static unsigned
hash(string) char *string; {
static unsigned hash(char* string)
{
register char *p;
register unsigned i,sum;
register unsigned i, sum;
if (strcmp(string,"ANY") == 0) return 128;
for (sum=i=0,p=string;*p;i += 3)
sum += (*p++)<<(i&03);
if (strcmp(string, "ANY") == 0)
return 128;
for (sum = i = 0, p = string; *p; i += 3)
sum += (*p++) << (i & 03);
return sum % 128;
}
void addtohashtable(char* s, int n)
{
/*
* Add a new pattern number to the hashtable.
* s is the key, n the pattern number
*/
unsigned hval;
register struct hlist *p;
addtohashtable(s,n) char *s; {
/*
* Add a new pattern number to the hashtable.
* s is the key, n the pattern number
*/
unsigned hval;
register struct hlist *p;
hval = hash(s);
p = (struct hlist *) malloc(sizeof *p);
p->h_patno = n;
/*
* Now insert in front of the list
* This way, the list will be sorted from high to low, which is the
* wrong way around, but easy
*/
p->h_next = hashtable[hval];
hashtable[hval] = p;
hval = hash(s);
p = (struct hlist *) malloc(sizeof *p);
p->h_patno = n;
/*
* Now insert in front of the list
* This way, the list will be sorted from high to low, which is the
* wrong way around, but easy
*/
p->h_next = hashtable[hval];
hashtable[hval] = p;
}
static
prhlist(p) struct hlist *p; {
/*
* Print a list in reversed order (see comment above)
*/
static void prhlist(struct hlist *p)
{
/*
* Print a list in reversed order (see comment above)
*/
if (p) {
prhlist(p->h_next);
fprintf(genc,"%d, ",p->h_patno - 1);
}
if (p)
{
prhlist(p->h_next);
fprintf(genc, "%d, ", p->h_patno - 1);
}
}
printhashtable() {
/*
* Print the linear lists, and also output an array of
* pointers to them
*/
register i;
register struct hlist *p;
void printhashtable(void)
{
/*
* Print the linear lists, and also output an array of
* pointers to them
*/
register int i;
for (i = 1; i <= 128; i++) {
fprintf(genc,"int hash%d[] = { ",i);
prhlist(hashtable[i-1]);
fputs("-1};\n",genc);
}
fputs("int hashany[] = { ", genc);
prhlist(hashtable[128]);
fputs("-1 };\n",genc);
fputs("int *hashtab[] = {\n",genc);
for (i = 1; i <= 128; i++) fprintf(genc,"\thash%d,\n",i);
fputs("\thashany\n};\n",genc);
for (i = 1; i <= 128; i++)
{
fprintf(genc, "int hash%d[] = { ", i);
prhlist(hashtable[i - 1]);
fputs("-1};\n", genc);
}
fputs("int hashany[] = { ", genc);
prhlist(hashtable[128]);
fputs("-1 };\n", genc);
fputs("int *hashtab[] = {\n", genc);
for (i = 1; i <= 128; i++)
fprintf(genc, "\thash%d,\n", i);
fputs("\thashany\n};\n", genc);
}

23
util/topgen/hash.h Normal file
View file

@ -0,0 +1,23 @@
/*
* hash.h
*
* Created on: 2018-11-24
* Author: carl
*/
#ifndef __HASH_H_INCLUDED__
#define __HASH_H_INCLUDED__
/*
* Add a new pattern number to the hashtable.
* s is the key, n the pattern number
*/
void addtohashtable(char* s, int n);
/*
* Print the linear lists, and also output an array of
* pointers to them
*/
void printhashtable(void);
#endif /* __HASH_H_INCLUDED__ */

View file

@ -11,6 +11,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "Lpars.h"
extern int lineno, newline;
@ -19,8 +20,10 @@ static int nerrors;
char *linedir = "#line %d \"%s\"\n"; /* format of line directive */
char *inpfile;
main(argc,argv) char *argv[]; {
extern void LLparse(void);
int main(int argc,char* argv[])
{
newline = 1;
if (argc != 3) {
fprintf(stderr,"Usage : %s targetoptimizerdescription outputdir\n",argv[0]);
@ -48,15 +51,16 @@ main(argc,argv) char *argv[]; {
}
/* VARARGS1 */
error(s, s1) char *s, *s1; {
void error(char *s, char* s1)
{
nerrors++;
fprintf(stderr,"\"%s\", line %d: ",inpfile,lineno);
fprintf(stderr,s,s1);
putc('\n',stderr);
}
onlyspace(s) register char *s; {
int onlyspace(register char* s)
{
while (*s) {
if (*s != ' ' && *s != '\t' && *s != '\n') return 0;

View file

@ -32,7 +32,8 @@ static struct pattern *pattable, /* ptr to pattern array */
* be allocated
*/
addpattern(str,l,np,nr) char *str; {
void addpattern(char* str,int l,int np,int nr)
{
/*
* Just add a pattern to the list.
* "str" is the constraint, "l" is the line number,
@ -62,8 +63,8 @@ addpattern(str,l,np,nr) char *str; {
p->p_nrepl = nr;
}
static
prconstraint(str) char *str; {
static void prconstraint(char* str)
{
/*
* prints a constraint, with variable names replaced
*/
@ -104,13 +105,13 @@ prconstraint(str) char *str; {
}
}
printpatterns() {
void printpatterns(void) {
/*
* Prints the pattern_descr table and generates the routine
* "check_constraint"
*/
register struct pattern *p;
register i;
register int i;
p = pattable;
i = 1;
@ -127,7 +128,7 @@ printpatterns() {
while (p < current) {
if (p->p_constraint) {
/* The pattern has a constraint */
fprintf(genc,"\tcase %d :\n",p - pattable);
fprintf(genc,"\tcase %d :\n",(int)(p - pattable));
fprintf(genc,linedir,p->p_lineno,inpfile); /* linedirective */
fputs("\tr = (",genc);
prconstraint(p->p_constraint);

27
util/topgen/pattern.h Normal file
View file

@ -0,0 +1,27 @@
/*
* pattern.h
*
* Created on: 2018-11-25
* Author: carl
*/
#ifndef __PATTERN_H_INCLUDED__
#define __PATTERN_H_INCLUDED__
/*
* Just add a pattern to the list.
* "str" is the constraint, "l" is the line number,
* "np" is the number of instructions in the pattern,
* "nr" is the number of instructions in the replacement
* Space is allocated in chunks of 50
*/
void addpattern(char* str,int l,int np,int nr);
/*
* Prints the pattern_descr table and generates the routine
* "check_constraint"
*/
void printpatterns(void);
#endif /* __PATTERN_H_INCLUDED__ */

View file

@ -15,6 +15,8 @@
struct symtab *idtable, *deftable;
extern void error(char *s, char* s1);
struct symtab *
findident(s, mode, table) char *s; struct symtab **table; {
/*
@ -24,7 +26,7 @@ findident(s, mode, table) char *s; struct symtab **table; {
* table yet, otherwise an error results
*/
register struct symtab *p;
register n;
register int n;
if (!*table) { /* No entry for this symbol */
if (mode == LOOKING) return (struct symtab *) 0;

View file

@ -21,6 +21,8 @@
#include "token.h"
#include "symtab.h"
#include "misc.h"
#include "hash.h"
#include "pattern.h"
char idbuf[BUFSIZ], buf[BUFSIZ];
int countid; /* # of variables */
@ -28,6 +30,13 @@ int countpat; /* # of patterns */
static int patlen; /* Maximum number of instructions in pattern */
static int maxoperand; /* Maximum number of operands of instruction */
extern FILE *input; /* file descriptor of inputfile */
extern int onlyspace(char* s);
extern void error(char *s, char* s1);
}
optim_description
@ -218,7 +227,7 @@ instruction(int opt;)
operand(',')
]*
{ if (count > maxoperand) {
error("Too many operands");
error("Too many operands","");
}
}
]?
@ -269,7 +278,7 @@ operand(int c;)
;
replacement (int *n;)
{register i;} :
{register int i;} :
SPACE*
{ *n = 0;}
[