as.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include <string.h>
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include "as.h"
  5. #include "arg_type.h"
  6. process_label() {}
  7. process_mnemonic( m)
  8. char *m;
  9. {
  10. for ( ; *m != '\0'; m++)
  11. if ( *m == '.')
  12. *m = '_';
  13. }
  14. process_operand( str, op)
  15. register char *str;
  16. register struct t_operand *op;
  17. {
  18. char *glob_lbl(), *strchr();
  19. op->type = 0;
  20. switch ( *str) {
  21. case '#' : op->type = IS_IMMEDIATE;
  22. op->expr = str+1;
  23. if ( *(op->expr) != '$') { /* #1 */
  24. op->val = atoi( str+1);
  25. if ( 1 <= op->val && op->val <= 8 ) {
  26. op->type = IS_QUICK;
  27. op->lbl = NULL;
  28. }
  29. }
  30. else
  31. if ( arg_type( str+1) == STRING) { /* #$1+$2 */
  32. op->lbl = op->expr;
  33. *(op->lbl+2) = '\0';
  34. op->expr = op->lbl+3;
  35. }
  36. else /* #$1 */
  37. op->lbl = NULL;
  38. break;
  39. case '(' : if ( strchr( str+1, ',') == NULL)
  40. if ( is_reg( str+1)) {
  41. op->reg = reg_val( str+1);
  42. if ( *(str+4) == '+') /* (sp)+ */
  43. op->type = IS_INCR;
  44. else /* (a0) */
  45. op->type = IS_IND_REG;
  46. }
  47. else {
  48. op->type = IS_IND_MEM;
  49. op->expr = str+1;
  50. for ( str++; *++str != ')';)
  51. ;
  52. *str = '\0';
  53. if ( *(op->expr) == '$')
  54. if ( arg_type( op->expr) == STRING) {
  55. /* ($1+$2) */
  56. op->lbl = op->expr;
  57. if ( strlen( op->lbl) == 2)
  58. op->expr = "0";
  59. else {
  60. *(op->lbl+2) = '\0';
  61. op->expr = op->lbl+3;
  62. }
  63. }
  64. else /* ($1) */
  65. op->lbl = NULL;
  66. else if ( isdigit( *(op->expr))) /* (1) */
  67. op->lbl = NULL;
  68. else { /* (.trppc) */
  69. op->lbl = glob_lbl( op->expr);
  70. op->expr = "0";
  71. }
  72. }
  73. else
  74. if ( *(str+1) == '[') {
  75. op->type = IS_IND_IND;
  76. op->expr = str+2;
  77. for ( str += 2; *++str != ',';)
  78. ;
  79. *str++ = '\0';
  80. for ( ; *str == ' '; str++)
  81. ;
  82. op->reg = reg_val( str);
  83. for ( ; *str++ != ']';)
  84. ;
  85. if ( *str == ')') /* ([$1, a6]) */
  86. op->expr2 = 0;
  87. else { /* ([8,a6],8) */
  88. for ( ; *str++ != ',';)
  89. ;
  90. for ( ; *str == ' '; str++)
  91. ;
  92. op->expr2 = atoi( str);
  93. }
  94. }
  95. else {
  96. op->expr = str+1;
  97. for ( str++; *++str != ',';)
  98. ;
  99. *str++ = '\0';
  100. for ( ; *str == ' '; str++)
  101. ;
  102. op->reg = reg_val( str);
  103. if ( *(str+2) == ')') /* (4, a0) */
  104. op->type = IS_IND_REG_DISPL;
  105. else { /* (0, sp, d0.l*1) */
  106. op->type = IS_3_OPS;
  107. for ( str++; *++str != ',';)
  108. ;
  109. for ( ; *str == ' '; str++)
  110. ;
  111. op->reg2 = reg_val( str);
  112. for ( ; *str++ != '*';)
  113. ;
  114. op->scale = atoi( str);
  115. }
  116. }
  117. break;
  118. case '-' : op->type = IS_DECR; /* -(sp) */
  119. op->reg = reg_val( str+2);
  120. break;
  121. case '$' : op->type = IS_GLOB_LBL; /* $1 */
  122. op->lbl = str;
  123. op->expr ="0";
  124. break;
  125. default : if ( is_reg( str)) {
  126. op->reg = reg_val( str);
  127. if ( *(str+2) == ':') { /* d2:d1 */
  128. op->type = IS_REG_PAIR;
  129. op->reg2 = reg_val( str+3);
  130. }
  131. else /* a6 */
  132. op->type = ( *str == 'd' ? IS_D_REG : IS_A_REG);
  133. }
  134. else if ( isdigit( *str)) { /* 1f */
  135. op->type = IS_LOC_LBL;
  136. op->lbl = str;
  137. op->expr = "0";
  138. *(str+1) ='\0';
  139. }
  140. else { /* .strhp */
  141. op->type = IS_GLOB_LBL;
  142. op->lbl = glob_lbl( str);
  143. *(str+1) ='\0';
  144. op->expr = "0";
  145. }
  146. }
  147. }
  148. int reg_val( reg)
  149. char *reg;
  150. {
  151. return( *reg == 's' ? 7 : atoi( reg+1));
  152. }
  153. int is_reg( str)
  154. register char *str;
  155. {
  156. switch ( *str) {
  157. case 'a' :
  158. case 'd' : return( isdigit( *(str+1)));
  159. case 's' : return( *(str+1) == 'p');
  160. default : return( 0);
  161. }
  162. }
  163. char *glob_lbl( lbl)
  164. char *lbl;
  165. {
  166. char *gl, *Malloc();
  167. gl = Malloc( strlen( lbl) + 3);
  168. sprintf( gl, "\"%s\"", lbl);
  169. return( gl);
  170. }
  171. /******************************************************************************/
  172. int mode_reg( eaddr)
  173. register struct t_operand *eaddr;
  174. {
  175. switch ( eaddr->type) {
  176. case IS_A_REG : return( 0x08 | eaddr->reg);
  177. case IS_D_REG : return( 0x00 | eaddr->reg);
  178. case IS_IND_REG : return( 0x10 | eaddr->reg);
  179. case IS_INCR : return( 0x18 | eaddr->reg);
  180. case IS_DECR : return( 0x20 | eaddr->reg);
  181. case IS_IND_MEM : return( 0x39);
  182. case IS_IND_IND : return( 0x30 | eaddr->reg);
  183. case IS_IND_REG_DISPL : return( 0x28 | eaddr->reg);
  184. case IS_GLOB_LBL : return( 0x39);
  185. case IS_3_OPS : if ( isdigit( *(eaddr->expr)) &&
  186. atoi( eaddr->expr) < 128)
  187. return( 0x30 | eaddr->reg);
  188. else
  189. fprintf( stderr, "FOUT in IS_3_OPS\n");
  190. break;
  191. case IS_QUICK :
  192. case IS_IMMEDIATE : return( 0x3c);
  193. default : fprintf( stderr,
  194. "mode_reg(), verkeerde operand %d\n",
  195. eaddr->type);
  196. abort();
  197. break;
  198. }
  199. }
  200. code_extension( eaddr)
  201. register struct t_operand *eaddr;
  202. {
  203. switch ( eaddr->type) {
  204. case IS_IND_MEM : if ( eaddr->lbl == NULL)
  205. @text4( %$( eaddr->expr));
  206. else
  207. @reloc4( %$( eaddr->lbl),
  208. %$( eaddr->expr),
  209. ABSOLUTE);
  210. break;
  211. case IS_IND_IND : if ( eaddr->expr2 == 0) {
  212. @text2( 0x161);
  213. @text2( %$(eaddr->expr));
  214. }
  215. else {
  216. @text2( 0x162);
  217. @text2( %$(eaddr->expr));
  218. @text2( %d(eaddr->expr2));
  219. }
  220. break;
  221. case IS_IND_REG_DISPL : @text2( %$( eaddr->expr));
  222. break;
  223. case IS_GLOB_LBL : @reloc4( %$(eaddr->lbl),
  224. %$(eaddr->expr),
  225. ABSOLUTE);
  226. break;
  227. case IS_3_OPS : if ( isdigit( *(eaddr->expr)) &&
  228. atoi( eaddr->expr) < 128) {
  229. @text2( %d( 0x0800 |
  230. ( eaddr->reg2 << 12) |
  231. ( two_log( eaddr->scale)<<9) |
  232. atoi( eaddr->expr)));
  233. }
  234. else
  235. fprintf( stderr, "FOUT in IS_3_OPS\n");
  236. break;
  237. case IS_QUICK :
  238. case IS_IMMEDIATE : if ( eaddr->lbl != NULL)
  239. @reloc4( %$(eaddr->lbl),
  240. %$(eaddr->expr),
  241. ABSOLUTE);
  242. else
  243. @text4( %$(eaddr->expr));
  244. }
  245. }
  246. int reg_mode( eaddr)
  247. struct t_operand *eaddr;
  248. {
  249. int mr;
  250. mr = mode_reg( eaddr);
  251. return( ((mr & 0x7) << 3) | ((mr & 0x38) >> 3));
  252. }
  253. code_opcode( opcode, field1, field2, eaddr)
  254. int opcode, field1, field2;
  255. struct t_operand *eaddr;
  256. {
  257. @text2( %d(((opcode & 0xf) << 12) | ((field1 & 0x7) << 9) |
  258. ((field2 & 0x7) << 6) | (mode_reg(eaddr) & 0x3f)));
  259. }
  260. code_instr( opcode, field1, field2, eaddr)
  261. int opcode, field1, field2;
  262. struct t_operand *eaddr;
  263. {
  264. if (eaddr->type == IS_IND_REG_DISPL) {
  265. @__instr_code(%d(((opcode & 0xf) << 12) | ((field1 & 0x7) << 9) |
  266. ((field2 & 0x7) << 6)),
  267. %d(eaddr->reg), %$(eaddr->expr));
  268. }
  269. else {
  270. code_opcode( opcode, field1, field2, eaddr);
  271. code_extension( eaddr);
  272. }
  273. }
  274. code_move( size, src, dst)
  275. int size;
  276. struct t_operand *src, *dst;
  277. {
  278. if (src->type == IS_IND_REG_DISPL) {
  279. if (dst->type == IS_IND_REG_DISPL) {
  280. @__moveXX(%d( ((size & 0x3) << 12)),
  281. %d(dst->reg), %$(dst->expr),
  282. %d(src->reg), %$(src->expr));
  283. }
  284. else {
  285. @__instr_code(%d( ((size & 0x3) << 12)|((reg_mode( dst) & 0x3f) << 6)),
  286. %d(src->reg), %$(src->expr));
  287. }
  288. }
  289. else if (dst->type == IS_IND_REG_DISPL) {
  290. @__move_X(%d( ((size & 0x3) << 12) | (mode_reg( src) & 0x3f)),
  291. %d(dst->reg), %$(dst->expr));
  292. }
  293. else {
  294. @text2( %d( ((size & 0x3) << 12) | ((reg_mode( dst) & 0x3f) << 6) |
  295. (mode_reg( src) & 0x3f)));
  296. code_extension( src);
  297. code_extension( dst);
  298. }
  299. }
  300. code_dist4( dst)
  301. struct t_operand *dst;
  302. {
  303. @reloc4( %$(dst->lbl), %$(dst->expr) + 4, PC_REL);
  304. }
  305. int two_log( nr)
  306. register int nr;
  307. {
  308. register int log;
  309. for ( log = 0; nr >= 2; nr >>= 1)
  310. log++;
  311. return( log);
  312. }