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

View file

@ -13,78 +13,84 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "misc.h" #include "misc.h"
#include "hash.h"
struct hlist { /* linear list of pattern numbers */ struct hlist
int h_patno; { /* linear list of pattern numbers */
struct hlist *h_next; int h_patno;
struct hlist *h_next;
}; };
static struct hlist *hashtable[129]; /* an array of ptr's to these lists, static struct hlist *hashtable[129]; /* an array of ptr's to these lists,
* the index in the array is the * the index in the array is the
* result of hashing * result of hashing
*/ */
static unsigned static unsigned hash(char* string)
hash(string) char *string; { {
register char *p; register char *p;
register unsigned i,sum; register unsigned i, sum;
if (strcmp(string,"ANY") == 0) return 128; if (strcmp(string, "ANY") == 0)
for (sum=i=0,p=string;*p;i += 3) return 128;
sum += (*p++)<<(i&03); for (sum = i = 0, p = string; *p; i += 3)
sum += (*p++) << (i & 03);
return sum % 128; 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; { hval = hash(s);
/* p = (struct hlist *) malloc(sizeof *p);
* Add a new pattern number to the hashtable. p->h_patno = n;
* s is the key, n the pattern number /*
*/ * Now insert in front of the list
unsigned hval; * This way, the list will be sorted from high to low, which is the
register struct hlist *p; * wrong way around, but easy
*/
hval = hash(s); p->h_next = hashtable[hval];
p = (struct hlist *) malloc(sizeof *p); hashtable[hval] = 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 static void prhlist(struct hlist *p)
prhlist(p) struct hlist *p; { {
/* /*
* Print a list in reversed order (see comment above) * Print a list in reversed order (see comment above)
*/ */
if (p) { if (p)
prhlist(p->h_next); {
fprintf(genc,"%d, ",p->h_patno - 1); prhlist(p->h_next);
} fprintf(genc, "%d, ", p->h_patno - 1);
}
} }
printhashtable() { void printhashtable(void)
/* {
* Print the linear lists, and also output an array of /*
* pointers to them * Print the linear lists, and also output an array of
*/ * pointers to them
register i; */
register struct hlist *p; register int i;
for (i = 1; i <= 128; i++) { for (i = 1; i <= 128; i++)
fprintf(genc,"int hash%d[] = { ",i); {
prhlist(hashtable[i-1]); fprintf(genc, "int hash%d[] = { ", i);
fputs("-1};\n",genc); prhlist(hashtable[i - 1]);
} fputs("-1};\n", genc);
fputs("int hashany[] = { ", genc); }
prhlist(hashtable[128]); fputs("int hashany[] = { ", genc);
fputs("-1 };\n",genc); prhlist(hashtable[128]);
fputs("int *hashtab[] = {\n",genc); fputs("-1 };\n", genc);
for (i = 1; i <= 128; i++) fprintf(genc,"\thash%d,\n",i); fputs("int *hashtab[] = {\n", genc);
fputs("\thashany\n};\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 <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "Lpars.h"
extern int lineno, newline; extern int lineno, newline;
@ -19,8 +20,10 @@ static int nerrors;
char *linedir = "#line %d \"%s\"\n"; /* format of line directive */ char *linedir = "#line %d \"%s\"\n"; /* format of line directive */
char *inpfile; char *inpfile;
main(argc,argv) char *argv[]; { extern void LLparse(void);
int main(int argc,char* argv[])
{
newline = 1; newline = 1;
if (argc != 3) { if (argc != 3) {
fprintf(stderr,"Usage : %s targetoptimizerdescription outputdir\n",argv[0]); fprintf(stderr,"Usage : %s targetoptimizerdescription outputdir\n",argv[0]);
@ -48,15 +51,16 @@ main(argc,argv) char *argv[]; {
} }
/* VARARGS1 */ /* VARARGS1 */
error(s, s1) char *s, *s1; { void error(char *s, char* s1)
{
nerrors++; nerrors++;
fprintf(stderr,"\"%s\", line %d: ",inpfile,lineno); fprintf(stderr,"\"%s\", line %d: ",inpfile,lineno);
fprintf(stderr,s,s1); fprintf(stderr,s,s1);
putc('\n',stderr); putc('\n',stderr);
} }
onlyspace(s) register char *s; { int onlyspace(register char* s)
{
while (*s) { while (*s) {
if (*s != ' ' && *s != '\t' && *s != '\n') return 0; if (*s != ' ' && *s != '\t' && *s != '\n') return 0;

View file

@ -32,7 +32,8 @@ static struct pattern *pattable, /* ptr to pattern array */
* be allocated * 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. * Just add a pattern to the list.
* "str" is the constraint, "l" is the line number, * "str" is the constraint, "l" is the line number,
@ -62,8 +63,8 @@ addpattern(str,l,np,nr) char *str; {
p->p_nrepl = nr; p->p_nrepl = nr;
} }
static static void prconstraint(char* str)
prconstraint(str) char *str; { {
/* /*
* prints a constraint, with variable names replaced * 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 * Prints the pattern_descr table and generates the routine
* "check_constraint" * "check_constraint"
*/ */
register struct pattern *p; register struct pattern *p;
register i; register int i;
p = pattable; p = pattable;
i = 1; i = 1;
@ -127,7 +128,7 @@ printpatterns() {
while (p < current) { while (p < current) {
if (p->p_constraint) { if (p->p_constraint) {
/* The pattern has a 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 */ fprintf(genc,linedir,p->p_lineno,inpfile); /* linedirective */
fputs("\tr = (",genc); fputs("\tr = (",genc);
prconstraint(p->p_constraint); 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; struct symtab *idtable, *deftable;
extern void error(char *s, char* s1);
struct symtab * struct symtab *
findident(s, mode, table) char *s; struct symtab **table; { 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 * table yet, otherwise an error results
*/ */
register struct symtab *p; register struct symtab *p;
register n; register int n;
if (!*table) { /* No entry for this symbol */ if (!*table) { /* No entry for this symbol */
if (mode == LOOKING) return (struct symtab *) 0; if (mode == LOOKING) return (struct symtab *) 0;

View file

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