www.pudn.com > miracl.zip > MEX.C
/* mex.c * * Updated to allow emission of scheduled code. * * Macro EXpansion program. * Expands Macros from a .mcs file into a .tpl file to create a .c file * * Copyright (c) 2002 Shamus Software Ltd. */ #include#include #include typedef int BOOL; #define FALSE 0 #define TRUE 1 /* Define Algorithms */ #define MULTIPLY 0 #define MULTUP 1 #define SQUARE 2 #define REDC 3 #define ADDITION 4 #define INCREMENT 5 #define SUBTRACTION 6 #define DECREMENT 7 #define SUMMATION 8 #define INCREMENTATION 9 #define DECREMENTATION 10 /* Define Macros */ #define MUL_START 0 #define STEP 1 #define STEP1M 2 #define STEP1A 3 #define STEP2M 4 #define STEP2A 5 #define MFIN 6 #define MUL_END 7 #define LAST 8 #define SQR_START 9 #define DSTEP 10 #define DSTEP1M 11 #define DSTEP1A 12 #define DSTEP2M 13 #define DSTEP2A 14 #define SELF 15 #define SFIN 16 #define SQR_END 17 #define REDC_START 18 #define RFINU 19 #define RFIND 20 #define REDC_END 21 #define ADD_START 22 #define ADD 23 #define ADD_END 24 #define SUB_START 25 #define SUB 26 #define SUB_END 27 #define INC_START 28 #define INC 29 #define INC_END 30 #define DEC_START 31 #define DEC 32 #define DEC_END 33 #define KADD_START 34 #define KASL 35 #define KADD_END 36 #define KINC_START 37 #define KIDL 38 #define KINC_END 39 #define KDEC_START 40 #define KDEC_END 41 #define LAST_ONE 42 BOOL scheduled; int PARAM; char *macro[LAST_ONE]; /* macro text */ char *functions[]={"MULTIPLY","MULTUP","SQUARE","REDC","ADDITION","INCREMENT", "SUBTRACTION","DECREMENT","SUMMATION","INCREMENTATION", "DECREMENTATION",NULL}; char *names[]={"MUL_START","STEP","STEP1M","STEP1A","STEP2M", "STEP2A","MFIN","MUL_END","LAST","SQR_START","DSTEP", "DSTEP1M","DSTEP1A","DSTEP2M","DSTEP2A","SELF", "SFIN","SQR_END","REDC_START","RFINU","RFIND", "REDC_END","ADD_START","ADD","ADD_END","SUB_START","SUB", "SUB_END","INC_START","INC","INC_END","DEC_START","DEC", "DEC_END","KADD_START","KASL","KADD_END","KINC_START","KIDL", "KINC_END","KDEC_START","KDEC_END",NULL}; BOOL white(char c) { if (c==' ' || c=='\n' || c=='\r' || c=='\t') return TRUE; else return FALSE; } int skip(char *c,int i) { while (white(c[i])) i++; return i; } int which(char *name,char *names[]) { int ipt=0; while (names[ipt]!=NULL) { if (strcmp(name,names[ipt])==0) return ipt; ipt++; } return -1; } void m_prologue(FILE *dotc,int k,int m) { fprintf(dotc,macro[STEP1M],k,m); } void m_epilogue(FILE *dotc,int x) { if (x==1) fprintf(dotc,macro[STEP1A]); else fprintf(dotc,macro[STEP2A]); } void m_schedule(FILE *dotc,int x,int k,int m) { if (x==1) { fprintf(dotc,macro[STEP2M],k,m); fprintf(dotc,macro[STEP1A]); } else { fprintf(dotc,macro[STEP1M],k,m); fprintf(dotc,macro[STEP2A]); } } void s_prologue(FILE *dotc,int k,int m) { fprintf(dotc,macro[DSTEP1M],k,m); } void s_epilogue(FILE *dotc,int x) { if (x==1) fprintf(dotc,macro[DSTEP1A]); else fprintf(dotc,macro[DSTEP2A]); } void s_schedule(FILE *dotc,int x,int k,int m) { if (x==1) { fprintf(dotc,macro[DSTEP2M],k,m); fprintf(dotc,macro[DSTEP1A]); } else { fprintf(dotc,macro[DSTEP1M],k,m); fprintf(dotc,macro[DSTEP2A]); } } /* Insert functions into template file */ void insert(int index,FILE *dotc) { int i,j,k,m,n,x; switch (index) { case MULTIPLY: fprintf(dotc,macro[MUL_START]); for (i=n=0;i4) { printf("Bad arguments\n"); printf("mex <.mcs file> <.tpl file>\n"); printf("Use flag -s for scheduled code\n"); printf("Examples:\n"); printf("mex 6 ms86 mrcomba\n"); printf("mex -s 8 c mrkcm\n"); exit(0); } ip=0; scheduled=FALSE; if (strcmp(argv[0],"-s")==0) { ip=1; scheduled=TRUE; } PARAM=atoi(argv[ip]); if (PARAM<4 || PARAM>32) { printf("Invalid parameter\n"); exit(0); } strcpy(fname,argv[ip+1]); strcat(fname,".mcs"); macros=fopen(fname,"r"); if (macros==NULL) { printf("Macro file %s not found\n",fname); exit(0); } strcpy(tmpl,argv[ip+2]); strcat(tmpl,".tpl"); template=fopen(tmpl,"r"); if (template==NULL) { printf("Template file %s file not found\n",tmpl); exit(0); } strcpy(tmpl,argv[ip+2]); strcat(tmpl,".c"); dotc=fopen(tmpl,"w"); if (dotc==NULL) { printf("Unable to open %s for output\n",tmpl); exit(0); } for (i=0;i