Pristine Ack-5.5
[Ack-5.5.git] / util / ceg / assemble / as_assemble / assemble.c
1 #include <ctype.h>
2 #include <system.h>
3 #include "em_decl.h"
4
5 /* This file contains the assemble routine that generates assembly code.
6  * As 'str' is in assembly format this is a easy job. Only operands 
7  * with "$n" in it need some special treatment. 
8  * Note : a '$' is qouted by prefixing it with a '$'.
9  */
10
11 assemble( str)
12 char *str;
13
14 /* Output assembly instruction. Substitute for '$n' the name of the
15  * the n-th argument of the current EM-instruction.
16  */
17 {
18         char buf[512] , *b_ptr, *arg_format();
19         int nr;
20
21         b_ptr = buf;
22
23         out( "fprint( codefile,\"");
24
25         while ( *str) {
26                 switch  ( *str) {
27                   case '$' :   if ( *(str+1) == '$') {
28                                         *b_ptr++ = '$';
29                                         str = str + 2;
30                                 }
31                                 else {  
32                                         nr = atoi( str+1) - 1;
33                                         *b_ptr = '\0';
34                                         out( "%s%s\", %s%s);", buf,
35                                                    arg_format( nr),
36                                                    C_instr_info->arg_type[nr] == ARITH ? "(long)" : "",
37                                                    C_instr_info->arg_conv[nr]);
38                                         out( "fprint( codefile,\"");
39                                         b_ptr = buf;
40                                         str = str + 2;
41                                 }
42                                 break;
43                   case '%':     *b_ptr++ = *str;
44                                 /* fall through */
45                   default  :    *b_ptr++ = *str++;
46                 }
47         }
48
49         *b_ptr = '\0';
50         out( "%s\\n\");\n", buf);
51 }
52
53
54 char *arg_format( nr)
55 int nr;
56 {
57         switch ( C_instr_info->arg_type[nr]) {
58           case ARITH : return( "%ld");
59           case STRING: return( "%s");
60           case INT   : return( "%d");
61         }
62         /*NOTREACHED*/
63 }