subr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #ifndef NORCSID
  2. static char rcsid[]= "$Header$";
  3. #endif
  4. #include "param.h"
  5. #include "reg.h"
  6. #include "lookup.h"
  7. #include "property.h"
  8. #include "expr.h"
  9. #include "set.h"
  10. #include "varinfo.h"
  11. #include "instruct.h"
  12. #include "token.h"
  13. #include "regvar.h"
  14. #include <cgg_cg.h>
  15. #include "extern.h"
  16. n_proc(name) char *name; {
  17. register symbol *sy_p;
  18. extern int npatbytes;
  19. sy_p = lookup(name,symproc,newsymbol);
  20. sy_p->sy_value.syv_procoff = npatbytes;
  21. }
  22. struct varinfo *
  23. make_erase(name) char *name; {
  24. expr_t e,ident_expr();
  25. struct varinfo *result;
  26. e = ident_expr(name);
  27. if (e.ex_typ != TYPREG)
  28. error("Register name required here");
  29. NEW(result,struct varinfo);
  30. result->vi_next = VI_NULL;
  31. result->vi_int[0] = e.ex_index;
  32. return(result);
  33. }
  34. n_instr(name,asname,oplist,eraselist,cost)
  35. char *name,*asname;
  36. operand *oplist;
  37. struct varinfo *eraselist,*cost;
  38. {
  39. register instrno;
  40. register cc_count;
  41. register instr_p ip;
  42. instrno = NEXT(ninstr,MAXINSTR,"Instructions");
  43. ip = &l_instr[instrno];
  44. ip->i_name = name;
  45. ip->i_asname = strlookup(asname!=0 ? asname : name);
  46. ip->i_nops = 0;
  47. ip->i_oplist = oplist;
  48. ip->i_erases = eraselist;
  49. if (cost==0) {
  50. ip->i_cost.ct_space = 0;
  51. ip->i_cost.ct_time = 0;
  52. } else {
  53. ip->i_cost.ct_space = cost->vi_int[0];
  54. ip->i_cost.ct_space = cost->vi_int[1];
  55. }
  56. for (cc_count=0; oplist!=0; oplist = oplist->o_next) {
  57. ip->i_nops++;
  58. if(oplist->o_adorn&AD_CC)
  59. cc_count++;
  60. }
  61. while (eraselist!=VI_NULL) {
  62. if (eraselist->vi_int[0] == -1 && cc_count)
  63. error("Instruction can't both set and break the condition codes");
  64. eraselist=eraselist->vi_next;
  65. }
  66. if (cc_count>1)
  67. error("No instruction can set condition codes more than once");
  68. }
  69. n_set(name,number) char *name; {
  70. register symbol *sy_p;
  71. sy_p = lookup(name,symset,newsymbol);
  72. sy_p->sy_value.syv_setno = number;
  73. }
  74. n_tok(name,atts,size,cost,format)
  75. char *name;
  76. struct varinfo *atts,*cost,*format;
  77. {
  78. register symbol *sy_p;
  79. register token_p tp;
  80. register struct varinfo *vip;
  81. int i;
  82. int tokno;
  83. char formstr[50],smallstr[2];
  84. sy_p = lookup(name,symtok,newsymbol);
  85. NEW(tp,token_t);
  86. tokno = NEXT(ntokens,MAXTOKENS,"Tokens");
  87. sy_p->sy_value.syv_tokno = tokno;
  88. l_tokens[tokno] = tp;
  89. tp->tk_name = sy_p->sy_name;
  90. tp->tk_size = size;
  91. if (cost != 0) {
  92. tp->tk_cost.ct_space = cost->vi_int[0];
  93. tp->tk_cost.ct_time = cost->vi_int[1];
  94. } else {
  95. tp->tk_cost.ct_space = 0;
  96. tp->tk_cost.ct_time = 0;
  97. }
  98. for(i=0,vip=atts;i<MAXATT && vip!=0;i++,vip=vip->vi_next) {
  99. tp->tk_att[i].ta_type = vip->vi_int[0];
  100. tp->tk_att[i].ta_name = vip->vi_str[0];
  101. vip->vi_str[0]=0;
  102. }
  103. if (i>maxtokensize)
  104. maxtokensize=i;
  105. if (vip!=0)
  106. error("More then %d attributes, rest discarded",MAXATT);
  107. for(;i<MAXATT;i++)
  108. tp->tk_att[i].ta_type= -3;
  109. if (format!=0) {
  110. formstr[0] = 0;
  111. for (vip=format;vip!=0;vip=vip->vi_next) {
  112. if (vip->vi_int[0]==0)
  113. strcat(formstr,vip->vi_str[0]);
  114. else {
  115. for(i=0;i<MAXATT;i++) {
  116. if (strcmp(vip->vi_str[0],tp->tk_att[i].ta_name)==0) {
  117. smallstr[0] = i+1;
  118. smallstr[1] = 0;
  119. strcat(formstr,smallstr);
  120. break;
  121. }
  122. }
  123. if (i==MAXATT)
  124. error("%s not a known attribute",
  125. vip->vi_str[0]);
  126. }
  127. }
  128. tp->tk_format = strlookup(formstr);
  129. } else
  130. tp->tk_format = -1;
  131. }
  132. checkprintformat(n) {
  133. register short *s;
  134. register i;
  135. extern set_t l_sets[];
  136. s= l_sets[n].set_val;
  137. for(i=nregs;i<nregs+ntokens;i++)
  138. if (BIT(s,i) && l_tokens[i-nregs]->tk_format<0)
  139. error("Token %s in set does not have printformat",
  140. l_tokens[i-nregs]->tk_name);
  141. }
  142. n_prop(name,size) char *name; int size; {
  143. int propno;
  144. register symbol *sp;
  145. propno = NEXT(nprops,MAXPROPS,"Properties");
  146. sp = lookup(name,symprop,newsymbol);
  147. sp->sy_value.syv_propno = propno;
  148. if (size <= 0) {
  149. error("Size of property must be >0");
  150. size = wordsize;
  151. }
  152. l_props[propno].pr_size = size;
  153. }
  154. prophall(n) {
  155. register i;
  156. short hallset[SETSIZE];
  157. for(i=0;i<SETSIZE;i++)
  158. hallset[i] = i<SZOFSET(MAXREGS) ? l_props[n].pr_regset[i] : 0;
  159. nexthall(hallset);
  160. }
  161. n_reg(name,printstring,nmemb,member1,member2) char *name,*printstring; {
  162. register symbol *sy_p;
  163. register reginfo *ri_p;
  164. int regno;
  165. sy_p = lookup(name,symreg,newsymbol);
  166. sy_p->sy_value.syv_regno = regno = NEXT(nregs,MAXREGS,"Number of registers");
  167. ri_p = &l_regs[regno];
  168. ri_p->ri_name = mystrcpy(name);
  169. ri_p->ri_repr = printstring!=0 ? mystrcpy(printstring) : ri_p->ri_name;
  170. ri_p->ri_memb[0] = member1;
  171. ri_p->ri_memb[1] = member2;
  172. if (nmemb>maxmembers)
  173. maxmembers=nmemb;
  174. return(regno);
  175. }
  176. make_const() {
  177. wordsize = cmustbeset("EM_WSIZE");
  178. pointersize = cmustbeset("EM_PSIZE");
  179. }
  180. cmustbeset(ident) char *ident; {
  181. return(lookup(ident,symconst,mustexist)->sy_value.syv_cstval);
  182. }
  183. n_const(ident,val) char *ident; {
  184. register symbol *sy_p;
  185. sy_p = lookup(ident,symconst,newsymbol);
  186. sy_p->sy_value.syv_cstval = val;
  187. }
  188. n_sconst(ident,val) char *ident,*val; {
  189. register symbol *sy_p;
  190. sy_p = lookup(ident,symsconst,newsymbol);
  191. sy_p->sy_value.syv_stringno = strlookup(val);
  192. }
  193. regline(rl,pl,rv) varinfo *rl,*pl; {
  194. register varinfo *rrl,*rpl;
  195. register short *sp;
  196. register reginfo *regp;
  197. int thissize;
  198. int propno;
  199. for(rrl=rl;rrl!=0;rrl=rrl->vi_next) {
  200. regp = &l_regs[rrl->vi_int[0]];
  201. thissize = 0;
  202. for(rpl=pl;rpl!=0;rpl=rpl->vi_next) {
  203. propno = rpl->vi_int[0];
  204. sp= l_props[propno].pr_regset;
  205. BIS(sp,rrl->vi_int[0]);
  206. if (thissize==0)
  207. thissize = l_props[propno].pr_size;
  208. else if (thissize!=-1 && thissize!=l_props[propno].pr_size)
  209. error("Register %s has no clear size",
  210. regp->ri_name);
  211. }
  212. regp->ri_size = thissize;
  213. regp->ri_class = regclass;
  214. regp->ri_rregvar = rv;
  215. if (rv>=0) {
  216. if (regp->ri_memb[0]!=0)
  217. error("Register variables may not have subregisters");
  218. rvused |= ANY_REGVAR;
  219. if (regp->ri_size == wordsize)
  220. rvused |= SL_REGVAR;
  221. else if (regp->ri_size == 2*wordsize)
  222. rvused |= DL_REGVAR;
  223. if (nregvar[rv]==0)
  224. rvsize[rv] = regp->ri_size;
  225. else if (rvsize[rv]!=regp->ri_size)
  226. error("All register variables of one type must have the same size");
  227. NEXT(nregvar[rv],MAXREGVAR,"Register variable");
  228. rvnumbers[rv][nregvar[rv]-1] = rrl->vi_int[0];
  229. }
  230. }
  231. regclass++;
  232. }
  233. setallreg(vi) struct varinfo *vi; {
  234. nallreg=0;
  235. for(;vi!=0;vi=vi->vi_next) {
  236. if (vi->vi_int[0]<0)
  237. continue;
  238. allreg[nallreg++] = vi->vi_int[0];
  239. }
  240. }
  241. freevi(vip) register struct varinfo *vip; {
  242. register i;
  243. extern char *end;
  244. if (vip==0)
  245. return;
  246. freevi(vip->vi_next);
  247. freevi(vip->vi_vi);
  248. for (i=0;i<VI_NSTR;i++)
  249. if (vip->vi_str[i]>end)
  250. free((char *) vip->vi_str[i]);
  251. free(vip);
  252. }
  253. int myatoi(s) register char *s; {
  254. register int base=10;
  255. register sum=0;
  256. if (*s=='0') {
  257. base = 8;
  258. s++;
  259. if (*s=='x') {
  260. base=16;
  261. s++;
  262. }
  263. }
  264. for (;;) {
  265. switch (*s) {
  266. default: return(sum);
  267. case '8':
  268. case '9':
  269. if (base==8) error("Bad digit in octal number");
  270. case '0':
  271. case '1':
  272. case '2':
  273. case '3':
  274. case '4':
  275. case '5':
  276. case '6':
  277. case '7':
  278. sum = sum*base + *s++ - '0';
  279. break;
  280. case 'a':
  281. case 'b':
  282. case 'c':
  283. case 'd':
  284. case 'e':
  285. case 'f':
  286. if (base!=16) error("Hexletter in number not expected");
  287. sum = sum*base + *s++ - 'a';
  288. break;
  289. case 'A':
  290. case 'B':
  291. case 'C':
  292. case 'D':
  293. case 'E':
  294. case 'F':
  295. if (base!=16) error("Hexletter in number not expected");
  296. sum = sum*base + *s++ - 'A';
  297. break;
  298. }
  299. }
  300. }
  301. char *mystrcpy(s) char *s; {
  302. register char *p;
  303. char *myalloc();
  304. p=myalloc(strlen(s)+1);
  305. strcpy(p,s);
  306. return(p);
  307. }
  308. char *myalloc(n) register n; {
  309. register char *p,*result;
  310. char *malloc();
  311. result=p=malloc(n);
  312. if (p== (char *) 0)
  313. fatal("Out of memory");
  314. do *p++=0; while (--n);
  315. return(result);
  316. }
  317. chkincl(value,lwb,upb) {
  318. if (value<lwb || value>upb)
  319. error("Number %d should have been between %d and %d",
  320. value,lwb,upb);
  321. return(value);
  322. }
  323. subset(sp1,sp2,setsize) short *sp1,*sp2; {
  324. register i;
  325. for(i=0;i<setsize;i++)
  326. if ( (sp1[i] | sp2[i]) != sp2[i])
  327. return(0);
  328. return(1);
  329. }
  330. vilength(vip) register struct varinfo *vip; {
  331. register l=0;
  332. while(vip!=0) {
  333. vip=vip->vi_next;
  334. l++;
  335. }
  336. return(l);
  337. }