home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / util / libs / reqtools / glue / gcc / gcc_glue.lha / fd2gcc / fd2gcc.y < prev    next >
Encoding:
Lex Description  |  1993-02-26  |  9.8 KB  |  529 lines

  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #ifdef sun
  7. #include <alloca.h>
  8. #endif
  9.  
  10. extern int      yylineno;
  11. extern char    *yytext;
  12. extern int      yylex();
  13. extern int      yyerror();
  14. extern int      yywarning();
  15.  
  16. #define STRLEN    256
  17. char            _identifyer[STRLEN];
  18. char           *identifyer = &_identifyer[0];
  19. char            base[STRLEN] = "";
  20. int             bias = -1;
  21. int             foundend = 0;
  22. int             _public = -1;
  23.  
  24. #define REGS    8
  25.  
  26. struct gcc_inline {
  27.     char            base[STRLEN];
  28.     int             bias;
  29.     int             _public;
  30.     char            function[STRLEN];
  31.     char            arguments[2 * REGS][STRLEN];
  32.     int             parameters[2 * REGS];
  33.     char            retval[STRLEN];
  34.     char            types[2 * REGS][STRLEN];
  35.     int             argument;
  36.     int             parameter;
  37.     int             type;
  38.     struct gcc_inline *prev;
  39. };
  40.  
  41. extern struct gcc_inline *curr;
  42. extern struct gcc_inline tmp;
  43. %}
  44.  
  45. %union {
  46.     char                 *sval;
  47.     struct gcc_inline    *gval;
  48. }
  49.  
  50. %type <gval> protofunc
  51. %type <sval> retval argtype struct identifyer builtin ptr varargs
  52.  
  53. %token    COMMENT
  54. %token    PRIVATE
  55. %token    PUBLIC
  56. %token    BASE
  57. %token    BIAS
  58. %token    END
  59. %token    NUMBER
  60. %token    IDENTIFYER
  61. %token    REGISTER
  62. %token    OPEN
  63. %token    CLOSE
  64. %token    ARGSEP
  65. %token    REGSEP
  66. %token    NEWLINE
  67.  
  68. %token    STRUCT
  69. %token    BUILTIN
  70. %token    VARARGS
  71. %token    PTR
  72. %token    SEMICOL
  73.  
  74. %%
  75.  
  76. fdfile        :    /* empty fdfile */
  77.         |    fdline fdfile
  78.     ;
  79.  
  80. fdline        :    commentline
  81.     {
  82.         if (foundend)
  83.             yyerror("commentline after end", yytext);
  84.     }
  85.         |    privateline
  86.     {
  87.         if (foundend)
  88.             yyerror("privateline after end", yytext);
  89.     }
  90.         |    _publicline
  91.     {
  92.         if (foundend)
  93.             yyerror("_publicline  after end", yytext);
  94.     }
  95.         |    baseline
  96.     {
  97.         if (foundend)
  98.             yyerror("baseline    after end", yytext);
  99.     }
  100.         |    biasline
  101.     {
  102.         if (foundend)
  103.             yyerror("biasline    after end", yytext);
  104.     }
  105.         |    endline
  106.     {
  107.         if (foundend)
  108.             yyerror("endline     after end", yytext);
  109.         foundend = 1;
  110.     }
  111.         |    funcline
  112.     {
  113.         struct gcc_inline *ptr;
  114.         if (!(ptr = (struct gcc_inline *) malloc(sizeof(struct gcc_inline))))
  115.             yyerror("!! out of memory !!", yytext);
  116.         memset(ptr, 0, sizeof(struct gcc_inline));
  117.         if (!strcmp(base, ""))
  118.             yyerror("no base              specified", yytext);
  119.         strcpy(tmp.base, base);
  120.         if (bias == -1)
  121.             yyerror("no bias              specified", yytext);
  122.         tmp.bias = bias;
  123.         if (_public == -1)
  124.             yyerror("no _public or private specified", yytext);
  125.         tmp._public = _public;
  126.         if (foundend)
  127.             yyerror("funcline    after end", yytext);
  128.         if (tmp.argument != tmp.parameter)
  129.             yywarning("#arguments != #parameters", yytext);
  130.         bias += 6;
  131.         memcpy(ptr, &tmp, sizeof(struct gcc_inline));
  132.         ptr->prev = curr;
  133.         curr = ptr;
  134.         memset(&tmp, 0, sizeof(struct gcc_inline));
  135.     }
  136.         |    prototype
  137.     {
  138.         memset(&tmp, 0, sizeof(struct gcc_inline));
  139.     }
  140.     ;
  141.  
  142. prototype :    retval protofunc params SEMICOL
  143.     {
  144.         struct gcc_inline *ptr = $2;
  145.         if (ptr) {
  146.             if (ptr->parameter != tmp.type)
  147.                 yywarning("#parameters != #types", yytext);
  148.             strcpy(ptr->retval, $1);
  149.             memcpy(ptr->types, tmp.types, 2 * REGS * STRLEN);
  150.         }
  151.     }
  152.     ;
  153.  
  154. retval : struct identifyer ptr
  155.     {
  156.         $$ = (char *) malloc(strlen($1) + 1 + strlen($2) + 1 + strlen($3) + 1);
  157.         if (!$$)
  158.             yyerror("!! out of memory !!", yytext);
  159.         strcpy($$, $1);
  160.         strcat($$, " ");
  161.         strcat($$, $2);
  162.         strcat($$, " ");
  163.         strcat($$, $3);
  164.         free($3);
  165.         free($2);
  166.         free($1);
  167.     }
  168.     |        struct identifyer
  169.     {
  170.         $$ = (char *) malloc(strlen($1) + 1 + strlen($2) + 1);
  171.         if (!$$)
  172.             yyerror("!! out of memory !!", yytext);
  173.         strcpy($$, $1);
  174.         strcat($$, " ");
  175.         strcat($$, $2);
  176.         free($2);
  177.         free($1);
  178.     }
  179.     |        builtin ptr
  180.     {
  181.         $$ = (char *) malloc(strlen($1) + 1 + strlen($2) + 1);
  182.         if (!$$)
  183.             yyerror("!! out of memory !!", yytext);
  184.         strcpy($$, $1);
  185.         strcat($$, " ");
  186.         strcat($$, $2);
  187.         free($2);
  188.         free($1);
  189.     }
  190.     |        builtin
  191.     {
  192.         $$ = (char *) malloc(strlen($1) + 1);
  193.         if (!$$)
  194.             yyerror("!! out of memory !!", yytext);
  195.         strcpy($$, $1);
  196.         free($1);
  197.     }
  198.     ;
  199.  
  200. protofunc : IDENTIFYER
  201.     {
  202.         struct gcc_inline *ptr = curr;
  203.         int             found = 0;
  204.         while (ptr) {
  205.             if (found = !strcmp(ptr->function, yytext))
  206.                 break;
  207.             ptr = ptr->prev;
  208.         }
  209.         if (!found) {
  210.             yywarning("fd for prototype not found", yytext);
  211.             $$ = 0;
  212.         } else
  213.             $$ = ptr;
  214.     }
  215.     ;
  216.  
  217. params    :    OPEN paramlist CLOSE
  218.     ;
  219.  
  220. paramlist    :    /* empty paramlist */
  221.         |    param ARGSEP paramlist
  222.         |    param
  223.     ;
  224.  
  225. param    :    argtype
  226.     {
  227.         strcpy(tmp.types[tmp.type], $1);
  228.         tmp.type++;
  229.         free($1);
  230.     }
  231.     ;
  232.  
  233. argtype : struct identifyer ptr dummy
  234.     {
  235.         $$ = (char *) malloc(strlen($1) + 1 + strlen($2) + 1 + strlen($3) + 1);
  236.         if (!$$)
  237.             yyerror("!! out of memory !!", yytext);
  238.         strcpy($$, $1);
  239.         strcat($$, " ");
  240.         strcat($$, $2);
  241.         strcat($$, " ");
  242.         strcat($$, $3);
  243.         free($3);
  244.         free($2);
  245.         free($1);
  246.     }
  247.     |        struct identifyer dummy
  248.     {
  249.         $$ = (char *) malloc(strlen($1) + 1 + strlen($2) + 1);
  250.         if (!$$)
  251.             yyerror("!! out of memory !!", yytext);
  252.         strcpy($$, $1);
  253.         strcat($$, " ");
  254.         strcat($$, $2);
  255.         free($2);
  256.         free($1);
  257.     }
  258.     |        builtin ptr dummy
  259.     {
  260.         $$ = (char *) malloc(strlen($1) + 1 + strlen($2) + 1);
  261.         if (!$$)
  262.             yyerror("!! out of memory !!", yytext);
  263.         strcpy($$, $1);
  264.         strcat($$, " ");
  265.         strcat($$, $2);
  266.         free($2);
  267.         free($1);
  268.     }
  269.     |        builtin dummy
  270.     {
  271.         $$ = (char *) malloc(strlen($1) + 1);
  272.         if (!$$)
  273.             yyerror("!! out of memory !!", yytext);
  274.         strcpy($$, $1);
  275.         free($1);
  276.     }
  277.     |        varargs
  278.     {
  279.         $$ = (char *) malloc(strlen($1) + 1);
  280.         if (!$$)
  281.             yyerror("!! out of memory !!", yytext);
  282.         strcpy($$, $1);
  283.         free($1);
  284.     }
  285.     ;
  286.  
  287. struct : STRUCT
  288.     {
  289.         $$ = (char *) malloc(strlen(yytext) + 1);
  290.         strcpy($$, yytext);
  291.     }
  292.     ;
  293.  
  294. identifyer : IDENTIFYER
  295.     {
  296.         $$ = (char *) malloc(strlen(identifyer) + 1);
  297.         strcpy($$, identifyer);
  298.     }
  299.     ;
  300.  
  301. builtin : BUILTIN
  302.     {
  303.         $$ = (char *) malloc(strlen(yytext) + 1);
  304.         strcpy($$, yytext);
  305.     }
  306.     ;
  307. ptr : PTR
  308.     {
  309.         $$ = (char *) malloc(strlen(yytext) + 1);
  310.         strcpy($$, yytext);
  311.     }
  312.     ;
  313. varargs : VARARGS
  314.     {
  315.         $$ = (char *) malloc(strlen(yytext) + 1);
  316.         strcpy($$, yytext);
  317.     }
  318.     ;
  319.  
  320. dummy : /* empty */
  321.     |    IDENTIFYER
  322.     |    OPEN PTR IDENTIFYER CLOSE OPEN dummyparamlist CLOSE
  323.     ;
  324.  
  325. dummyparamlist    :    /* empty dummyparamlist */
  326.         |    dummyparam ARGSEP paramlist
  327.         |    dummyparam
  328.     ;
  329.  
  330. dummyparam : dummystruct dummyidentifyer dummyptr dummy
  331.     |        dummystruct dummyidentifyer dummy
  332.     |        dummybuiltin dummyptr dummy
  333.     |        dummybuiltin dummy
  334.     |        dummyvarargs
  335.     ;
  336.  
  337. dummystruct : STRUCT
  338.     ;
  339.  
  340. dummyidentifyer : IDENTIFYER
  341.     ;
  342.  
  343. dummybuiltin : BUILTIN
  344.     ;
  345. dummyptr : PTR
  346.     ;
  347. dummyvarargs : VARARGS
  348.     ;
  349.  
  350. commentline    :    COMMENT
  351.     ;
  352.  
  353. privateline    :    PRIVATE NEWLINE
  354.     {
  355.         _public = 0;
  356.     }
  357.     ;
  358.  
  359. _publicline    :    PUBLIC NEWLINE
  360.     {
  361.         _public = 1;
  362.     }
  363.     ;
  364.  
  365. baseline    :    BASE base NEWLINE
  366.     ;
  367.  
  368. base        :    IDENTIFYER
  369.     {
  370.         strcpy(base, yytext);
  371.     }
  372.     ;
  373.  
  374. biasline    :    BIAS bias NEWLINE
  375.     ;
  376.  
  377. bias        :    NUMBER
  378.     {
  379.         bias = atoi(yytext);
  380.     }
  381.     ;
  382.  
  383. endline        :    END NEWLINE
  384.     ;
  385.  
  386. funcline    :    function arguments registers NEWLINE
  387.     ;
  388.  
  389. function    :    IDENTIFYER
  390.     {
  391.         strcpy(tmp.function, identifyer);
  392.     }
  393.     ;
  394.  
  395. arguments    :    OPEN argumentlist CLOSE
  396.     ;
  397.  
  398. argumentlist    :    /* empty argumentlist */
  399.         |    argument ARGSEP argumentlist
  400.         |    argument
  401.     ;
  402.  
  403. argument    :    IDENTIFYER
  404.     {
  405.         strcpy(tmp.arguments[tmp.argument], identifyer);
  406.         tmp.argument++;
  407.     }
  408.     ;
  409.  
  410. registers    :    OPEN registerlist CLOSE
  411.     ;
  412.  
  413. registerlist    :    /* empty registerlist */
  414.         |    register separator registerlist
  415.         |    register
  416.     ;
  417.  
  418. separator    :    REGSEP
  419.         |    ARGSEP
  420.     ;
  421.  
  422. register    :    REGISTER
  423.     {
  424.         if (tmp.parameter < tmp.argument) {
  425.             int             reg = atoi(&yytext[1]);
  426.             if (toupper(yytext[0]) == 'A')
  427.                 reg += 8;
  428.             tmp.parameters[tmp.parameter] = reg;
  429.         }
  430.         tmp.parameter++;
  431.     }
  432.     ;
  433.  
  434. %%
  435.  
  436. /* syntax part of .fd file parsing     */
  437.  
  438. yywarning(s, at)
  439.     char           *s;
  440.     char           *at;
  441. {
  442.     fprintf(stderr, "**** yacc WARNING   **** line: %ld, <%s> at [%s]\n",
  443.         yylineno + 1, s, at);
  444. }
  445. yyerror(s, at)
  446.     char           *s;
  447.     char           *at;
  448. {
  449.     fprintf(stderr, "**** yacc ERROR   **** line: %ld, <%s> at [%s]\n",
  450.         yylineno + 1, s, at);
  451.     exit(1);
  452. }
  453.  
  454. struct gcc_inline *curr = 0;
  455. struct gcc_inline tmp;
  456.  
  457. void 
  458. findregs(ptr, regs)
  459.     struct gcc_inline *ptr;
  460.     int            *regs;
  461. {
  462.     int             r;
  463.     int             p;
  464.     for (r = 0; r < 2 * REGS; r++)
  465.         *(regs + r) = 0;
  466.     for (p = 0; p < ptr->parameter; p++)
  467.         *(regs + ptr->parameters[p]) = 1;
  468. }
  469. main()
  470. {
  471.     memset(&tmp, 0, sizeof(struct gcc_inline));
  472.     if (!yyparse())
  473.         while (curr) {
  474.             int             r;
  475.             int             p;
  476.             int             regs[2 * REGS];
  477.             struct gcc_inline *ptr = curr;
  478.             if (ptr->_public) {
  479.                 int             noretval = (!strcmp(ptr->retval, "void")) || (!strcmp(ptr->retval, "VOID"));
  480.                 printf("static __inline %s\n", ptr->retval);
  481.                 printf("%s (BASE_PAR_DECL", ptr->function);
  482.                 for (p = 0; p < ptr->parameter; p++)
  483.                     printf(" %s %s%s", ptr->types[p], ptr->arguments[p], (p < ptr->parameter - 1) ? "," : "");
  484.                 printf(")\n");
  485.                 printf("{\n");
  486.                 printf("  BASE_EXT_DECL\n");
  487.                 if (!noretval)
  488.                     printf("  register %s _res  __asm(\"d0\");\n", ptr->retval);
  489.                 printf("  register struct %s * a6 __asm(\"a6\") = BASE_NAME;\n", &ptr->base[1]);
  490.                 for (p = 0; p < ptr->parameter; p++) {
  491.                     int             no = ptr->parameters[p];
  492.                     int             reg = 'd';
  493.                     if (no >= REGS) {
  494.                         no -= REGS;
  495.                         reg = 'a';
  496.                     }
  497.                     printf("  register %s %c%d __asm(\"%c%d\") = %s;\n", ptr->types[p], reg, no, reg, no, ptr->arguments[p]);
  498.                 }
  499.                 printf("  __asm __volatile (\"jsr a6@(-%d)\"\n", ptr->bias);
  500.                 printf("  : %s\n", noretval ? "/* no output */" : "\"=r\" (_res)");
  501.                 printf("  : \"r\" (a6),");
  502.                 for (p = 0; p < ptr->parameter; p++) {
  503.                     int             no = ptr->parameters[p];
  504.                     int             reg = 'd';
  505.                     if (no >= REGS) {
  506.                         no -= REGS;
  507.                         reg = 'a';
  508.                     }
  509.                     printf(" \"r\" (%c%d)%s", reg, no, (p < ptr->parameter - 1) ? "," : "");
  510.                 }
  511.                 printf("\n");
  512.                 findregs(ptr, regs);
  513.                 printf("  :");
  514.                 for (r = REGS; r < 2 * REGS; r++)
  515.                     if (regs[r])
  516.                         printf(" \"a%d\",", r - REGS);
  517.                 for (r = 0; r < REGS; r++)
  518.                     if (regs[r])
  519.                         printf(" \"d%d\",", r);
  520.                 printf("  \"memory\");\n");
  521.                 if (!noretval)
  522.                     printf("  return _res;\n");
  523.                 printf("}\n");
  524.             }
  525.             curr = ptr->prev;
  526.             free(ptr);
  527.         }
  528. }
  529.