objasm 29.7 KB
Newer Older
1 2
/*objasm.c - output an objasm file given a module defn*/

3
/*OSLib---efficient, type-safe, transparent, extensible,\n"
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   register-safe A P I coverage of RISC O S*/
/*Copyright  1994 Jonathan Coxhead*/

/*
      OSLib is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 1, or (at your option)
   any later version.

      OSLib is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

      You should have received a copy of the GNU General Public License
   along with this programme; if not, write to the Free Software
   Foundation, Inc, 675 Mass Ave, Cambridge, MA 02139, U S A.
*/

/*From CLib*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*From OSLib*/
#include "os.h"
#include "osfile.h"
#include "taskwindow.h"
#include "wimp.h"

/*From Support*/
#include "lookup.h"
#include "trace.h"

/*Local*/
#include "def.h"
#include "main.h"
#include "objasm.h"

45
#define MEM_FOR_OBJASM (1024*1024)
46

47
static char *Local;
48

49
static char
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   *APCS_Names [] =
   /*names of registers as seen by A P C S*/
   {  /*arguments*/
      "A1", "A2", "A3", "A4",

      /*variables*/
      "V1", "V2", "V3", "V4", "V5", "V6",

      /*fixed purpose*/
      "SL", "FP", "IP", "SP", "LR", "PC"
   },

   *SWI_Names [] =
   /*names of registers as seen by SWI call*/
   {  /*general*/
      "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9",

      /*fixed purpose*/
      "SL", "FP", "IP", "SP", "LR", "PC"
   };
/*-----------------------------------------------------------------------*/

72
static char *Op
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
(
   def_op op
)
{
   switch (op)
   {
      case def_OP_DISJOINS:
         return "ORR";
      break;

      case def_OP_CONJOINS:
         return "AND";
      break;

      case def_OP_ADDS:
         return "ADD";
      break;

      case def_OP_EXCLUSIVELY_DISJOINS:
         return "EOR";
      break;
   }

   return SKIP;
}
/*-----------------------------------------------------------------------*/
static int Set_Count
(
   bits set
)
{
   int n, c = 0;

   for (n = 0; n < 32; n++)
      if ((set & 1 << n) != 0)
         c++;

   return c;
}
/*-----------------------------------------------------------------------*/

static bool Set_Elem
(
   int  n,
   bits set
)
{
   return (set & 1 << n) != NONE;
}
/*-----------------------------------------------------------------------*/

/*Is type |t| 1 byte wide?*/

static bool Byte_Wide
(
   def_t t,
   lookup_t types
)
{
   def_t t1;

   switch (t->tag)
   {
      case def_TYPE_CHAR: case def_TYPE_BYTE:
         return TRUE;
      break;

      case def_TYPE_ID:
         if (lookup (types, t->data AS id, (void **) &t1) == NULL)
            /*It's a locally defined type - use its definition.*/
            return Byte_Wide (t1, types);
         else if (lookup (main_byte_wide, t->data AS id, NULL) == NULL)
            /*It's in the byte-wide table.*/
            return TRUE;
         else
         {
            /*Give a warning and assume it's a word.*/
            fprintf (stderr, "warning: type \"%s\" not defined in this "
                  "module - assuming it's a word\n", t->data AS id);
            return FALSE;
         }
      break;

      default:
         return FALSE;
      break;
   }
}
/*-----------------------------------------------------------------------*/

static int Emit_Header
(
   FILE *file,
166 167
   char *title,
   char *author
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
)
{
   time_t now;

   return
      fprintf
      (
         file,
         ";ObjAsm file for %s\n"
         ";written by DefMod (%s) on %s"
         ";%s\n"
         "\n"
         "R0      RN      0\n"
         "R1      RN      1\n"
         "R2      RN      2\n"
         "R3      RN      3\n"
         "R4      RN      4\n"
         "R5      RN      5\n"
         "R6      RN      6\n"
         "R7      RN      7\n"
         "R8      RN      8\n"
         "R9      RN      9\n"
         "\n"
         "A1      RN      0\n"
         "A2      RN      1\n"
         "A3      RN      2\n"
         "A4      RN      3\n"
         "V1      RN      4\n"
         "V2      RN      5\n"
         "V3      RN      6\n"
         "V4      RN      7\n"
         "V5      RN      8\n"
         "V6      RN      9\n"
         "\n"
         "R       RN      0\n"
         "\n"
         "SL      RN      10\n"
         "FP      RN      11\n"
         "IP      RN      12\n"
         "SP      RN      13\n"
         "LR      RN      14\n"
         "PC      RN      15\n"
         "\n",
         title,
         __DATE__,
         (time (&now), ctime (&now)),
         author
      );
}
/*-----------------------------------------------------------------------*/

static int Emit0
(
   FILE *file,
222
   char *s0
223 224 225 226 227 228 229 230 231 232 233 234 235
)
{
   int rc = 0;

   rc = fprintf (file, "%-7s %s\n", Local != NULL? Local: "", s0);
   Local = NULL;
   return rc;
}
/*-----------------------------------------------------------------------*/

static int Emit1
(
   FILE *file,
236 237
   char *s0,
   char *s1
238 239 240 241 242 243 244 245 246 247 248 249 250 251
)
{
   int rc = 0;

   rc = fprintf (file, "%-7s %-7s %s\n",
         Local != NULL? Local: "", s0, s1);
   Local = NULL;
   return rc;
}
/*-----------------------------------------------------------------------*/

static int Emit2
(
   FILE *file,
252 253 254
   char *s0,
   char *s1,
   char *s2
255 256 257 258 259 260 261 262 263 264 265 266 267 268
)
{
   int rc = 0;

   rc = fprintf (file, "%-7s %-7s %s, %s\n",
         Local != NULL? Local: "", s0, s1, s2);
   Local = NULL;
   return rc;
}
/*-----------------------------------------------------------------------*/

static int Emit3
(
   FILE *file,
269 270 271 272
   char *s0,
   char *s1,
   char *s2,
   char *s3
273 274 275 276 277 278 279 280 281 282 283 284 285 286
)
{
   int rc = 0;

   rc = fprintf (file, "%-7s %-7s %s, %s, %s\n",
         Local != NULL? Local: "", s0, s1, s2, s3);
   Local = NULL;
   return rc;
}
/*-----------------------------------------------------------------------*/

static int Emit_Label
(
   FILE *file,
287
   char *s0
288 289 290 291 292 293 294 295 296
)
{
   return fprintf (file, "%s ROUT\n", s0);
}
/*-----------------------------------------------------------------------*/

static int Emit_Local
(
   FILE *file,
297
   char *s0
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
)
{
   file = file;

   Local = s0;
   return 0;
}
/*-----------------------------------------------------------------------*/

static int Emit_Line
(
   FILE *file
)
{
   return fprintf (file, "\n");
}
/*-----------------------------------------------------------------------*/

static int Emit_Comment
(
   FILE *file,
   char *comment
)
{
   return fprintf (file, ";%s\n", comment);
}
/*-----------------------------------------------------------------------*/

static int Format_APCS_List
(
   char *s0,
   bits  set,
   bool  hat
)
{
   char *cc = s0;
   int n, rc = 0;

   for (n = 0; n < 16; n++)
      if (Set_Elem (n, set))
      {
         if ((rc = sprintf (cc, cc == s0? "{%s": ", %s", APCS_Names [n]))
               < 0)
            return rc;

         cc += strlen (cc);
      }

   return sprintf (cc, cc == s0? "{}%s": "}%s", hat? "^": "");
}
/*-----------------------------------------------------------------------*/

static int Format_SWI_List
(
   char *s0,
   bits  set,
   bool  hat
)
{
   char *cc = s0;
   int n, rc = 0;

   for (n = 0; n < 16; n++)
      if (Set_Elem (n, set))
      {
         if ((rc = sprintf (cc, cc == s0? "{%s": ", %s", SWI_Names [n]))
               < 0)
            return rc;

         cc += strlen (cc);
      }

   return sprintf (cc, cc == s0? "{}%s": "}%s", hat? "^": "");
}
/*-----------------------------------------------------------------------*/

static int Emit_Code
(
   FILE    *file,
377
   char    *swi,
378 379
   def_s    s,
   bool     nonx,
380 381
   bool     apcs32,
   lookup_t types
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
)
{
   char c_name [def_ID_LIMIT + 1],
              c_plus_plus_name [def_C_PLUS_PLUS_LIMIT + 1], s1 [127 + 1],
              s2 [127 + 1];
   bits o, v, a, p, w; /*sets*/
   int  ni, no, nc, nk, nv, na, np, nw, /*set counts*/
              n, m, /*menials*/
              f, rc = 0;
   enum {LR_IN_IP, LR_STACKED, LR_PRESERVED} mode;
   bool       use_block;

   Local = NULL;

   def_as_extern (c_name + (!nonx? 1: 0), swi);
#if 0
   def_as_c_plus_plus (c_plus_plus_name + (!nonx? 1: 0), swi, s);
#endif
   if (!nonx) c_name [0] = c_plus_plus_name [0] = 'x';

   /*Count the various set sizes*/
   ni = Set_Count (s->i);
   nc = Set_Count (s->c);
   nk = Set_Count (s->k);

   /*We handle the case of result registers by

         (a) deleting the result bit from the output argument set (s->o,
      s->f), so that the calculations of numbers of arguments work;
         (b) emitting a 'MOV R, result' at the end.
   */
   o = s->o, f = s->f_out;
   if (nonx)
   {
      if (!Set_Elem (def_FLAGS, s->value))
         o &= ~s->value;
      else
         f = 0;
   }
   no = Set_Count (o);

   /*The set of variable registers that must be saved by the function.*/
   v = (s->i | o | s->c | s->k | s->value) & ~(1 << 15 | 0xF);
      /*need never save R0, R1, R2, R3 or PC*/
   nv = Set_Count (v);

   /*The set of argument registers that are going to be needed as addresses
      at which to store results, and the set of such registers that are
      preserved by the SWI.*/
   a = NONE;
   p = NONE;
   for (n = 0; n < 4; n++)
      if (ni <= n && n < ni + no + f)
      {
         /*This is an output argument.*/
         if (Set_Elem (n, s->i | o | s->c | s->k | s->value))
            /*This is corrupted by the SWI (or the veneer).*/
            a |= 1 << n;
         else
            /*This is preserved.*/
            p |= 1 << n;
      }
   na = Set_Count (a);
   np = Set_Count (p);

   mode = ni > 4? LR_STACKED: nonx? LR_PRESERVED: LR_IN_IP;
      /*assume that the non-x form can only be called in user mode*/

   use_block = def_using_block (s);

   if ((rc = Emit_Label (file, c_name)) < 0)
      goto finish;
#if 0
   if ((rc = Emit_Label (file, c_plus_plus_name)) < 0)
      goto finish;
#endif

   if (mode == LR_STACKED)
   {
      if ((rc = Emit2 (file, "MOV", "IP", "SP")) < 0)
         goto finish;
   }
   else if (mode == LR_IN_IP)
   {
      if ((rc = Emit2 (file, "MOV", "IP", "LR")) < 0)
         goto finish;
   }

   if (!use_block)
   {
      /*The set of registers that we can use as workspace because: they
         are not needed on input; they are not set or corrupted on
         output; they need not be preserved by A P C S.*/
      w = 0xFu & ~s->i & ~o & ~s->c & ~s->k & ~s->value & ~p;
      nw = Set_Count (w);
            /*Be careful not to use them until their contents are used!*/

      if (nv + na > 0)
      {
         if ((rc = Format_APCS_List (s1,
               v | a | (mode == LR_STACKED? 1 << 14: 0), FALSE)) < 0)
            goto finish;

         if ((rc = Emit2 (file, "STMFD", "SP!", s1)) < 0)
            goto finish;
      }

      if (ni > 4)
      {
         /*We have to make a set of all but the first 4 input arguments
            (which are in registers already)*/
         bits set = NONE;
         int count = 0;

         for (n = 0; n < 16; n++)
            if (Set_Elem (n, s->i) && count++ >= 4)
               set |= 1 << n;

         if ((rc = Format_SWI_List (s1, set, FALSE)) < 0)
            goto finish;
         if ((rc = Emit2 (file, "LDMFD", "IP", s1)) < 0)
            goto finish;
      }

      for (m = 3; m >= 0; m--)
         if (ni > m)
            if ((n = def_bit_index (s->i, m)) != m)
               if ((rc = Emit2 (file, "MOV", SWI_Names [n],
                     APCS_Names [m])) < 0)
                  goto finish;
   }
   else
   {
      /*Much easier - save all the arguments and load the (unique)
         input register with the location they've been saved to. Only
         then can you save any variables.*/

      /*The set of registers that we can use as workspace because: they
         are not needed on input; they are not set or corrupted on
         output; they need not be preserved by A P C S.*/
      w = NONE;
      nw = 0;

      if ((rc = Emit2 (file, "STMFD", "SP!", "{A1, A2, A3, A4}")) < 0)
         goto finish;
         /*SP is NOW pointing at the block to be passed to the SWI*/

      if (nv > 0)
      {
         if ((rc = Format_APCS_List (s1, v, FALSE)) < 0)
            goto finish;

         if ((rc = Emit2 (file, "STMFD", "SP!", s1)) < 0)
            goto finish;
      }

      if ((rc = sprintf (s1, "#%d", 4*nv)) < 0)
         goto finish;

      for (n = 0; n < 10; n++)
         if (Set_Elem (n, s->i))
            if ((rc = Emit3 (file, "ADD", SWI_Names [n], "SP", s1)) < 0)
               goto finish;
      /*we know that LR is in IP*/
   }

   if (nk > 0)
      for (n = 9; n >= 0; n--)
         if (Set_Elem (n, s->k))
         {
            if (Set_Elem (n, s->i))
            {
               int  j, low;
               bits k = s->constants [n];

                  /*If there is an input register, OP the constant with it
                     instead of moving it in, where OP is the operation in
                     the SWI definition. This fails badly with AND, so it's
                     not supported.*/
               low = 0;
               while (TRUE)
               {
                  /*Find the bottom 1 bit of |k| not yet emitted.*/
                  m = 0;
                  for (j = low; j < 32; j += 2)
                     if ((k & 3 << j) != NONE)
                     {
                        m = k & 0xFF << j;
                        low = j + 8;
                        break;
                     }

                  if (m == 0) break; /*no more bits*/

                  if ((rc = sprintf (s2, "#&%X", m)) < 0)
                     goto finish;

                  if ((rc = Emit3 (file, Op (s->op [n]), SWI_Names [n],
                        SWI_Names [n], s2)) < 0)
                     goto finish;
               }
            }
            else
            {
               int  j, low;
               bits k = s->constants [n];
               bool first = TRUE, negative = Set_Count (k) > 16;

               if (negative) k = ~k;
               low = 0;
               while (TRUE)
               {
                  /*Find the bottom 1 bit of |k| not yet emitted.*/
                  m = 0;
                  for (j = low; j < 32; j += 2)
                     if ((k & 3 << j) != NONE)
                     {
                        m = k & 0xFF << j;
                        low = j + 8;
                        break;
                     }

                  if (m == 0) break; /*no more bits*/

                  if ((rc = sprintf (s2, "#&%X", m)) < 0)
                     goto finish;

                  if (first)
                  {
                     if ((rc = Emit2 (file, !negative? "MOV": "MVN",
                           SWI_Names [n], s2)) < 0)
                        goto finish;
                     first = FALSE;
                  }
                  else
                  {
                     if ((rc = Emit3 (file, !negative? "ORR": "BIC",
                           SWI_Names [n], SWI_Names [n], s2)) < 0)
                        goto finish;
                  }
               }

               if (first)
                  /*If we escaped with no effect yet, the register must be
                     cleared.*/
                  if ((rc = Emit2 (file, !negative? "MOV": "MVN",
                        SWI_Names [n], "#0")) < 0)
                     goto finish;
            }
         }

#if 0
   /*Experimental!*/
   for (n = 0; n < 4; n++)
      if (Set_Elem (n, w))
         /*Trash register |n| now, just to show we can do it.*/
         if ((rc = Emit2 (file, "MOV", SWI_Names [n], "#&FF0")) < 0)
            goto finish;
#endif

   /*Registers now set up properly - do the business part.*/
   if ((rc = sprintf (s1, "&%X", s->swi | (!nonx? 1 << 17: 0))) < 0)
      goto finish;

   if ((rc = Emit1 (file, "SWI", s1)) < 0)
      goto finish;

#if 0
   /*Experimental!*/
   for (n = 0; n < 4; n++)
      if (Set_Elem (n, w))
      {  /*Check that register |n| still has its contents.*/
         if ((rc = Emit2 (file, "TEQ", SWI_Names [n], "#&FF0")) < 0)
            goto finish;
         if ((rc = Emit2 (file, "MOVNE", "PC", "#0")) < 0)
            goto finish;
      }
#endif

   /*All over bar the shouting ...*/
   if (no + f > 0)
   {
      if (!nonx)
         if ((rc = Emit1 (file, "BVS", "%99")) < 0)
            goto finish;

      for (n = 0; n < no + f; n++)
      {
         int reg;
         bool store_byte;

         /*Do we want a STRNE or a STRNEB?*/
         if (n != no)
         {
            reg = def_bit_index (o, n);
            store_byte = !Set_Elem (reg, s->ro) &&
                  Byte_Wide (s->outputs [reg], types);
         }
         else
         {
682 683 684 685 686 687 688 689 690 691 692 693 694 695
            if (apcs32)
            {
               reg = (nonx && !Set_Elem (0, s->value)) ? 0 : 1;

               if ((rc = Emit2 (file, "MOV", SWI_Names [reg], "PC")) < 0)
                  goto finish;
               if ((rc = Emit2 (file, "MRS", SWI_Names [reg], "CPSR")) < 0)
                  goto finish;
            }
            else
            {
               reg = 15; /*PC*/
            }

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
            store_byte = FALSE;
         }

         /*Output argument |n| is in parameter position |ni + n|. Is this
            safe?*/
         if (Set_Elem (ni + n, p))
         {
            /*The address is still where it was on entry, undisturbed.*/
            if ((rc = Emit2 (file, "TEQ", APCS_Names [ni + n], "#0")) < 0)
               goto finish;

            if ((rc = sprintf (s1, "[%s]", APCS_Names [ni + n])) < 0)
               goto finish;
            if ((rc = Emit2 (file, !store_byte? "STRNE": "STRNEB",
                  SWI_Names [reg], s1)) < 0)
               goto finish;
         }
         else
         {
            /*The address has been stacked.*/
            int offset, i;
717
            char *tmp = mode == LR_PRESERVED? "IP": "LR";
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

            /*If the argument number that this argument has is in the first
               4, then it has been stacked above. Otherwise, it has been
               stacked by A P C S, and we have stacked |nv| variables and
               |na| other addresses (and maybe LR) above it.*/
            if (ni + n < 4)
            {
               offset = 0;
               for (i = 0; i < n; i++)
                  if (Set_Elem (ni + i, a))
                     offset += 4;
            }
            else
               offset = 4*(ni + n - 4 +
                     nv + na + (mode == LR_STACKED? 1: 0));

            if ((rc = sprintf (s1, "[SP, #%d]", offset)) < 0)
               goto finish;
            if ((rc = Emit2 (file, "LDR", tmp, s1)) < 0)
               goto finish;

            if ((rc = Emit2 (file, "TEQ", tmp, "#0")) < 0)
               goto finish;

            if ((rc = sprintf (s1, "[%s]", tmp)) < 0)
               goto finish;
            if ((rc = Emit2 (file, !store_byte? "STRNE": "STRNEB",
                  SWI_Names [reg], s1)) < 0)
               goto finish;
         }
      }

      if (!nonx)
      {
         if ((rc = Emit2 (file, "MOV", "R", "#0")) < 0)
            goto finish;
         if ((rc = Emit_Local (file, "99")) < 0)
            goto finish;
      }
      else
      {
         /*Value to return?*/
760 761
         if (!(s->value == NONE || Set_Elem (0, s->value)))
         {
762
            /*If no value is wanted, or if R0 is required, leave R alone.*/
763 764 765 766 767 768 769 770 771 772 773 774 775 776
            if (apcs32 && Set_Elem(def_FLAGS, s->value))
            {
               if ((rc = Emit2 (file, "MOV", "R", "PC")) < 0)
                  goto finish;
               if ((rc = Emit2 (file, "MRS", "R", "CPSR")) < 0)
                  goto finish;
            }
            else
            {
               if ((rc = Emit2 (file, "MOV", "R",
                     SWI_Names [def_bit_index (s->value, 0)])) < 0)
                  goto finish;
            }
         }
777 778 779 780 781 782 783 784 785 786 787 788 789
      }
   }
   else
   {
      /*No output registers to worry about.*/
      if (!nonx)
      {
         if ((rc = Emit2 (file, "MOVVC", "R", "#0")) < 0)
            goto finish;
      }
      else
      {
         /*Value to return?*/
790 791
         if (!(s->value == NONE || Set_Elem (0, s->value)))
         {
792
            /*If no value is wanted, or if R0 is required, leave R alone.*/
793 794 795 796 797 798 799 800 801 802 803 804 805 806
            if (apcs32 && Set_Elem(def_FLAGS, s->value))
            {
               if ((rc = Emit2 (file, "MOV", "R", "PC")) < 0)
                  goto finish;
               if ((rc = Emit2 (file, "MRS", "R", "CPSR")) < 0)
                  goto finish;
            }
            else
            {
               if ((rc = Emit2 (file, "MOV", "R",
                     SWI_Names [def_bit_index (s->value, 0)])) < 0)
                  goto finish;
            }
         }
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
      }
   }

   /*The tidy up code*/
   if (!use_block)
   {
      if (na > 0)
      {
         if ((rc = sprintf (s1, "#%d", 4*na)) < 0)
            goto finish;
         if ((rc = Emit3 (file, "ADD", "SP", "SP", s1)) < 0)
            goto finish;
      }

      if (nv > 0)
      {
         if ((rc = Format_APCS_List (s1,
               v | (mode == LR_STACKED? 1 << 15: 0),
825
               /*hat?*/ mode == LR_STACKED && !apcs32)) < 0)
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
            goto finish;
         if ((rc = Emit2 (file, "LDMFD", "SP!", s1)) < 0)
            goto finish;
      }
   }
   else
   {
      /*Done in a different order in this case. First restore variables,
         if any.*/
      if (nv > 0)
      {
         if ((rc = Format_APCS_List (s1, v, FALSE)) < 0)
            goto finish;

         if ((rc = Emit2 (file, "LDMFD", "SP!", s1)) < 0)
            goto finish;
      }

      /*Then skip over the saved four arguments.*/
      if ((rc = Emit3 (file, "ADD", "SP", "SP", "#16")) < 0)
         goto finish;
   }

   if (mode == LR_IN_IP)
   {
851
      if ((rc = Emit2 (file, apcs32? "MOV": "MOVS", "PC", "IP")) < 0)
852 853 854 855
         goto finish;
   }
   else if (mode == LR_PRESERVED)
   {
856
      if ((rc = Emit2 (file, apcs32? "MOV": "MOVS", "PC", "LR")) < 0)
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
         goto finish;
   }

   if (nw > 0)
   {
      if ((rc = Format_SWI_List (s1, w, FALSE)) < 0)
         goto finish;
      sprintf (s2, "Registers available for scratch use: %s", s1);
      if ((rc = Emit_Comment (file, s2)) < 0)
         goto finish;
   }

   if ((rc = Emit_Line (file)) < 0)
      goto finish;

finish:
   return rc;
}
/*-----------------------------------------------------------------------*/

os_error *objasm_output
(
   FILE     *file,
   char     *title,
   char     *author,
   lookup_t  needs,
   lookup_t  consts,
   lookup_t  types,
   lookup_t  swis,
   bool      separate, /*if |separate|, output is to the directory |output|; otherwise,
                          to the file |file|.*/
888 889
   bool      apcs32,
   char     *output
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
)
{
   os_error *error = NULL;
   char     *swi;
   def_s     s;
   void     *context;
   int       rc = 0;
   bool      done_fopen_via_file = FALSE, done_fopen_object = FALSE;
   FILE     *via_file = NULL, *object = NULL /*for Norcroft*/;

   needs = needs, consts = consts, types = types;

   if (!separate)
   {
      if ((rc = Emit_Header (file, title, author)) < 0)
         goto finish;

      /*Emit all EXPORT statements.*/
      context = 0;
      while (TRUE)
      {
         char c_name [def_ID_LIMIT + 1],
                    c_plus_plus_name [def_C_PLUS_PLUS_LIMIT + 1];

         if ((error = lookup_enumerate (swis, &swi, (void **) &s,
               &context)) != NULL)
            goto finish;
         if (context == 0) break;

         if (!s->absent)
         {
            /*Export the symbol with a preceding 'x.'*/
            def_as_extern (c_name + 1, swi);
            c_name [0] = 'x';
            #if 0
            def_as_c_plus_plus (c_plus_plus_name + 1, swi, s);
            #endif
            c_plus_plus_name [0] = 'x';

            if ((rc = Emit1 (file, "EXPORT", c_name)) < 0)
               goto finish;
            if ((rc = Emit1 (file, "EXPORT", c_name + 1)) < 0)
               goto finish;
#if 0
            if ((rc = Emit1 (file, "EXPORT", c_plus_plus_name)) < 0)
               goto finish;
            if ((rc = Emit1 (file, "EXPORT", c_plus_plus_name + 1)) < 0)
               goto finish;
#endif
         }
      }
      if ((rc = Emit_Line (file)) < 0)
         goto finish;

      if ((rc = Emit2 (file, "AREA", "|SWI$$Code|", "CODE, READONLY, PIC")) < 0)
         goto finish;
      if ((rc = Emit_Line (file)) < 0)
         goto finish;

      /*Emit code.*/
      context = 0;
      while (TRUE)
      {
         if ((error = lookup_enumerate (swis, &swi, (void **) &s,
               &context)) != NULL)
            goto finish;

         if (context == 0) break;

         if (!s->absent)
         {
961
            if ((rc = Emit_Code (file, swi, s, FALSE, apcs32, types)) < 0)
962
               goto finish;
963
            if ((rc = Emit_Code (file, swi, s, TRUE, apcs32, types)) < 0)
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
               goto finish;
         }

         if (def_inlinable (s))
            if ((rc = fprintf (stderr, "information: SWI %s could be "
                  "inlined\n", swi)) < 0)
               goto finish;
      }

      if ((rc = Emit0 (file, "END")) < 0)
         goto finish;
   }
   else
   {
      char name1 [FILENAME_MAX + 1], name2 [FILENAME_MAX + 1],
                 cmd [1024];
980
      int  count = 0;
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

      /*Ensure the directory exists.*/
      if ((error = xosfile_create_dir (output, 0)) != NULL)
         goto finish;

#if 0
      /*Get the prefix - needed because we'll be starting a subtask.*/
      if ((error = xos_read_var_val ("Prefix$Dir", prefix_dir,
            FILENAME_MAX, context1, os_VARTYPE_EXPANDED, &len, &context1,
            &var_type)) != NULL)
         goto finish;

      if (len == 0)
         strcpy (prefix_dir, "@");
      else
         prefix_dir [len] = '\0';
#endif

      if ((via_file = fopen ("ViaFile", "w+")) == NULL)
      {
         error = _kernel_last_oserror ();
         goto finish;
      }
      done_fopen_via_file = TRUE;

      context = 0;
      while (TRUE)
      {
         char c_name [def_ID_LIMIT + 1],
                    c_plus_plus_name [def_C_PLUS_PLUS_LIMIT + 1],
                    dir_name [FILENAME_MAX + 1];
         bool nonx;

         if ((error = lookup_enumerate (swis, &swi, (void **) &s,
               &context)) != NULL)
            goto finish;
         if (context == 0) break;

         if (!s->absent)
         {
            for (nonx = FALSE; nonx <= TRUE; nonx++)
            {
               def_as_extern (c_name + (!nonx? 1: 0), swi);
#if 0
               def_as_c_plus_plus (c_plus_plus_name +
                     (!nonx? 1: 0), swi, s);
#endif
               if (!nonx) c_name [0] = c_plus_plus_name [0] = 'x';

               /*Get the SWI number truncated to 4 bits.*/
               if ((rc = sprintf (dir_name, "0x%.5X%s", s->swi & 0xDFFF0,
                     !nonx? "x": "")) < 0)
                  goto finish;
               if ((rc = sprintf (name1, "%s.%s", output, dir_name)) < 0)
                  goto finish;
               if ((error = xosfile_create_dir (name1, 0)) != NULL)
                  goto finish;

               if ((rc = sprintf (name1, "%s.%s.s", output, dir_name)) < 0)
                  goto finish;
               if ((error = xosfile_create_dir (name1, 0)) != NULL)
                  goto finish;
               if ((rc = sprintf (name2, "%s.%s.o", output, dir_name)) < 0)
                  goto finish;
               if ((error = xosfile_create_dir (name2, 0)) != NULL)
                  goto finish;

               if ((rc = sprintf (name1, "%s.%s.s.%.3d", output, dir_name,
                     count)) < 0)
                  goto finish;
               if ((rc = sprintf (name2, "%s.%s.o.%.3d", output, dir_name,
                     count)) < 0)
                  goto finish;

               if ((object = fopen (name1, "w")) == NULL)
               {
                  error = _kernel_last_oserror ();
                  goto finish;
               }
               done_fopen_object = TRUE;

               if ((rc = Emit_Header (object, swi, author)) < 0)
                  goto finish;

               if ((rc = Emit1 (object, "EXPORT", c_name)) < 0)
                  goto finish;
#if 0
               if ((rc = Emit1 (object, "EXPORT", c_plus_plus_name)) < 0)
                  goto finish;
#endif

               if ((rc = Emit_Line (object)) < 0)
                  goto finish;

               if ((rc = Emit2 (object, "AREA",
                     "|SWI$$Code|", "CODE, READONLY, PIC")) < 0)
                  goto finish;
               if ((rc = Emit_Line (object)) < 0)
                  goto finish;

1081
               if ((rc = Emit_Code (object, swi, s, nonx, apcs32, types)) < 0)
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
                  goto finish;

               if ((rc = Emit0 (object, "END")) < 0)
                  goto finish;

               if (fclose (object) == EOF)
               {
                  error = _kernel_last_oserror ();
                  goto finish;
               }
               done_fopen_object = FALSE;

#if 0
               if ((rc = sprintf (cmd, "ObjAsm -from %s.%s -to %s.%s "
1096 1097
                     "-stamp -quit -apcs 3/%dbit/SWstackcheck", prefix_dir,
                     name1, prefix_dir, name2, apcs32 ? 32 : 26))
1098 1099 1100
                     < 0)
                  goto finish;
#else
1101 1102 1103
               if ((rc = sprintf (cmd, "ObjAsm -from %s -to %s "
                     "-stamp -quit -apcs 3/%dbit/SWstackcheck",
                     name1, name2, apcs32 ? 32 : 26)) < 0)
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
                  goto finish;
#endif

               tracef ("%s ...\n" _ cmd);
               if ((rc = printf ("ObjAsm %s\n", name1)) < 0)
                  goto finish;

               if (system (cmd) != 0)
               {
                  error = _kernel_last_oserror ();
                  goto finish;
               }
               tracef ("%s ... done\n" _ cmd);

               if ((rc = fprintf (via_file, "%s\n", name2)) < 0)
                  goto finish;
            }
         }

         count++;
      }
   }

finish:
   if (rc < 0) error = _kernel_last_oserror ();

   if (done_fopen_object && fclose (object) == EOF && error == NULL)
      error = _kernel_last_oserror ();

   if (done_fopen_via_file && fclose (via_file) == EOF && error == NULL)
      error = _kernel_last_oserror ();

   return error;
}