home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / programm / language / bcpl4ami.lzh / bcpl / booting.doc next >
Encoding:
Text File  |  1988-03-24  |  18.3 KB  |  393 lines

  1.  
  2.  
  3.  Bootstrapping the BCPL Compiler using INTCODE
  4.  ---------------------------------------------
  5.  
  6.  
  7.  
  8.  by M. Richards
  9.  
  10.  
  11.  
  12.  Abstract:
  13.  ---------
  14.  
  15.  
  16.  For a compiler written in its own language, there is the
  17.  problem of choosing a good strategy for bootstrapping it onto
  18.  a new machine.  The methed explored in this paper is the
  19.  preferred mechanism for transferring BCPL and involves the use
  20.  of an interpretive machine code called INTCODE.  INTCODE is
  21.  designed specifically for this purpose.  Its design and the
  22.  general strategy of using it in a transfer are described.
  23.  
  24.  
  25.  
  26.  Computer Laboratory,
  27.  University of Cambridge,
  28.  Corn Exchange Street,
  29.  Cambridge,
  30.  England                              August 1973
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  Bootstrapping the BCPL Compiler using INTCODE
  37.  ---------------------------------------------
  38.  
  39.  
  40.  The portability of a programming language is strongly influenced
  41.  by its design, the structure of its compiler and the mechanism used
  42.  to transfer it from one machine to another.  Although the prime
  43.  concern of this paper is to discuss a method of easing the boot-
  44.  strapping problem, it is in order to survey the effects on a language
  45.  of requiring it to portable, since decisions in this area have
  46.  a considerable bearing on the subsequent bootstrapping problem.
  47.  
  48.  A programming language is always a compromise between the
  49.  differing and usually conflicing requirements of a large number
  50.  of constraints and design aims.  For instance, one often wishes
  51.  to incorporate powerful high-level facilities into a language
  52.  without, at the same time, jeopardising the efficiency of the
  53.  compiled code.  Alternatively, one may be under pressure (from
  54.  users) to provide language extensions at a time when the compiler
  55.  is already too large to fit comfortably in the machine.
  56.  
  57.  The main effect of the portability constraint on a language
  58.  is a reduction in the number of primitive facilities provided and
  59.  the removal of most machine dependencies.  Small machine
  60.  independent languages are inherently portable, and only gross
  61.  errors in compiler design will prevent such languages from being
  62.  transferred easily.  It is worth noting that much of the effort
  63.  required to transfer a compiler is in the rewriting of the code-
  64.  generator for the new machine, and that the size of the machine
  65.  independent parts of the compiler are of little relevance.  This
  66.  suggests that portability is enhanced mainly by reducing the more
  67.  fundamental facilities of the language such as the variety of
  68.  data and storage types, the complexity of the calling mechanism
  69.  for procedures, and the number of primitive expression operators,
  70.  while many of the higher level features such as conditional
  71.  commands and the scope rules of identifiers may, on the other
  72.  hand, be left in without portability suffering significantly.
  73.  BCPL í1,2ó was designed to be inherently portable and, as a
  74.  result, it has rather few primitive facilities.  It has, for
  75.  instance, only one data type, three storage types, a very simple
  76.  procedure calling mechanism, and few expression operators, and it
  77.  is possible to describe the language in terms of a very simple
  78.  abstract machine whose machine code is a simple and natural inter-
  79.  face between the machine independent part of the compiler and the
  80.  code generator. Since there is only one data type, all variables,
  81.  values of expressions, anonymous results, and arguments are of the
  82.  same size and it is reasonable for the allocation of space for
  83.  items in the run-time stack to be done by the machine independent
  84.  part of the compiler, with a consequent simplification of the
  85.  code-generator and an improvement in portability.  The language
  86.  has a wide variety of non-primitive linguistic facilities such
  87.  as conditional commands and syntactic constructions to reduce
  88.  the need for GOTO commands, but the only expression operators
  89.  available correspond to the fixed point, logical and relational
  90.  instructions common to most computers.  Two additional operators
  91.  provide facilities for forming and using machine addresses, and
  92.  since these operators, like all the others, cannot check the
  93.  types of their operands, they are dangerously powerful.  In many
  94.  respests, BCPL can be regarded as a clean machine code in
  95.  high level notation.
  96.  
  97.  
  98.  The interface language - OCODE
  99.  ------------------------------
  100.  
  101.  OCODE í3ó is the name of the assembly language for the
  102.  abstract BCPL machine.  Its design is important since it is the
  103.  interface language between the first phase of the compiler and
  104.  the code-generator, and, like any other language, it must satisfy
  105.  a number of constraints, the main one being that it must be
  106.  capable of efficient code-generation.  The OCODE form of an
  107.  expression is basically the reverse polish translation with
  108.  separate OCODE statements for each operation.  For example, if
  109.  x, y and z are local variables in positions 4, 5 and 6 of the
  110.  current stack frame, then the OCODE translation of x/y + z
  111.  would be:
  112.  
  113.      LP 4 LP 5 DIV LP 6 PLUS
  114.  
  115.  There are three fundamental operations for BCPL local variables:
  116.  loading the value, loading the address of the variable and up-
  117.  dating the variable, and LP, LLP and SP are the corresponding
  118.  OCODE keywords.  Similarly, the other two storage types, global
  119.  and static, each have three OCODE statements for their trans-
  120.  lation.  Thus, there are only 9 statements, in all, for accessing
  121.  variables;  in addition to these, there are 19 for the arithmetic,
  122.  relational and logical primitives, and one for indirection which
  123.  is also used for subscripted expressions and data structure
  124.  selection.  There are 5 statements for loading the various kinds
  125.  of explicit constants available in the language, and remaining
  126.  statements are mainly directives to the code-generator, or are
  127.  concerned with procedure calls and jumps.  Thus, the abstract
  128.  BCPL machine can be programmed in a language containing fewer
  129.  than 60 different simple statements.
  130.  
  131.  It is instructive, at this stage, to consider the effect of
  132.  language extensions on the complexity of OCODE.  We have seen
  133.  already that each storage type requires three OCODE statements;
  134.  however, for each additional numerical data type in the language
  135.  the effect is far more disastrous.  We would require three new
  136.  statements for each of the storage types and about 12 new statements
  137.  for expression operators defined for the new data type.  Unfort-
  138.  unately, the situation is likely to be even worse than this since
  139.  it may be necessary to leave the space allocation to the code-
  140.  generator which will, in consequence, require a more complex
  141.  version of OCODE and a proportional increase in effort required to
  142.  write the code-generator. For a BCPL-like language extended to
  143.  contain real and long-real arithmetic, one would expect the
  144.  corresponding OCODE to contain nearly 120 different statements.
  145.  Many applications do not require real arithmetic and the
  146.  improvement in portability resulting from its omission is attractive.
  147.  
  148.  OCODE makes no provision for optimisation based on the
  149.  analysis of the flow structure of the program, but optimisation
  150.  at the local level is certainly possible and is performed by most
  151.  code-generators.  Particular care was taken in the design of the
  152.  OCODE primitives for procedure definitions and calls so that
  153.  there would be as wide a choice as possible in the details of
  154.  the actual calling mechanism used.
  155.  
  156.  Before INTCODE was developed, OCODE was the basis of the
  157.  mechanism used to transfer the compiler to a new machine.  At that
  158.  time, the bootstrapping kit consisted of the source form of the
  159.  compiler and a character representation of the corresponding
  160.  OCODE form.  To bootstrap the compiler, one first had to write a
  161.  simple non-optimising code-generator for OCODE and then use it
  162.  to generate code for the entire compiler from its OCODE form
  163.  supplied in the kit.  The first stage of the bootstrap was
  164.  completed by combining this code with suitable interface
  165.  routines to provide input, output and other operating system
  166.  facilities.  An optimising code-generator for the new machine
  167.  could then be produced by suitably modifying an already
  168.  existing one for some other machine;  this being far less work
  169.  than writing one from scratch.
  170.  
  171.  OCODE is thus effective not only as an interface between
  172.  the two halves of the compiler, but also as the basis of a
  173.  method of bootstrapping.  However, after completing several
  174.  transfers using OCODE, it was found that the bootstrapping
  175.  capability could be improved.  OCODE makes more provision for
  176.  optimisation than is neessary for bootstrapping purposes and,
  177.  although a simple code-generator could be written, it required
  178.  more knowledge and understanding of BCPL than was absolutely
  179.  necessary.  Thus, when the implementation of the bootstrap code-
  180.  generator was undertaken by a programmer with no previous
  181.  experience with BCPL, it often took longer than expectec and
  182.  frequently contained strategic errors in design.  The solution
  183.  was to take OCODE and to compile it into the assembly language
  184.  of a second, even simpler, machine code for the BCPL abstract
  185.  machine.  The assembly language that was designed for this
  186.  purpose is called INTCODE and it could be used in place of OCODE
  187.  in the BCPL kit.
  188.  
  189.  
  190.  The INTCODE machine
  191.  -------------------
  192.  
  193.  Unlike a conventional computer, the INTCODE machine is not
  194.  fully specified, and such details as the word-length, byte-size,
  195.  and instruction format are left undefined.  The machine has 6
  196.  control registers as follows:  A and B are accumulators for
  197.  computing expressions, C is the sequence control register giving
  198.  the location of the next instruction to be obeyed, D is a register
  199.  used to hold the computed address of the current instruction, and
  200.  P and G are index registers.  All these registers are the size of
  201.  a machine word.
  202.  
  203.  An instruction has a 3 bit function field, and an address
  204.  field of unspecified size, 2 bits for index modification and an
  205.  indirection bit.  These fields may be laid out in the word in
  206.  any way that is convenient for the interpreter.  An instruction
  207.  is executed as follows.  Firstly, it is fetched from the store
  208.  and C is incremented, then, the computed address is formed by
  209.  assigning the address field to D, conditionally adding P or G
  210.  as specified by the modification field, and indirecting if
  211.  required.  Finally, the operation specified by the function
  212.  field is performed.
  213.  
  214.  The 8 machine functions are: LOAD, ADD, STORE, JUMP,
  215.  JUMP ON TRUE, JUMP OF FALSE, CALL, and EXECUTE OPERATION, and they
  216.  are denoted in the assembly language by the single mnemonic
  217.  letters L, A, S, J, T, F, K, and X, respectively.  LOAD will
  218.  assign the computed address to A after saving its previous contents
  219.  in B.  ADD will add D to A, and STORE will assign A to the storage
  220.  location addressed by D.  The effect of JUMP is to assign D to C,
  221.  thus causing a transfer of control.  JUMP ON TRUE and JUMP ON FALSE
  222.  are conditional transfer instructions that test the value held in
  223.  A.  For these instructions, zero represents false and any non-zero
  224.  value represents true.  CALL is used in the compilation of a BCPL
  225.  function or routine call.  It increments P by the amount specified
  226.  in D, saves the old value of P and the return address, and then
  227.  jumps to the entry point held in A.  The final instruction
  228.  EXECUTE OPERATION provides a miscellaneous collection of arithmetic,
  229.  relational, logical, and control functions, the actual function
  230.  being determined by D. Most of the functions operate on B and
  231.  A, usually leaving a result in A.  For example, X7 will cause the
  232.  remainder after the integer division of B by A to be assigned to
  233.  A.  There are 23 execute operations in the basic INTCODE machine,
  234.  but for practical use, a further 5 to 10 are needed in order to
  235.  provide an adequate interface with the operating system.
  236.  
  237.  The assembly form of an INTCODE instruction consists of the
  238.  mnemonic letter for the function, followed by 'I' if indirection
  239.  is specified, followed by 'P' or 'G' if P or G modification is
  240.  specified, and finally followed by the address.  The address is
  241.  either given explicitly as a decimal integer or as a reference
  242.  to a label.  A label reference is denoted by 'L' followed by
  243.  the label number.  A number not preceded by a letter is
  244.  interpreted as a label setting directive and causes the specified
  245.  label to be set to the address of the next item to be assembled.
  246.  As an example, the following piece of BCPL program:
  247.  
  248.         IF SW DO X := 126
  249.         Y := Y REM X
  250.  
  251.  could be translated into the following INTCODE:
  252.  
  253.         LIG103                / load SW
  254.         FL73                  / jump on false to label 73
  255.         L126                  / load 126
  256.         SP3                   / assign to X
  257.         73                    / set label 73
  258.         LIP4                  / load Y
  259.         LIP3                  / load X
  260.         X7                    / form remainder
  261.         SP4                   / assign to Y
  262.  
  263.  In this example, SW is assumed to global 103, and X and Y
  264.  to be the third and fourth local variables.
  265.  
  266.  Data may be assembled using various data statements.  For
  267.  instance, the statement D163 will cause a word to be allocated
  268.  with initial value 163, and DL46 will allocate a word holding
  269.  the value of label 46 as initial value.  String data can be
  270.  assembled using character statements;  for instance, the BCPL
  271.  string "ABC" might compile into:
  272.  
  273.          LL493    ...
  274.          493  C3  C65  C66  C67
  275.          ...
  276.  
  277.  In this example, the instructions LL493 will load the address of
  278.  a region of store where the bytes 3, 65, 66, and 67, representing
  279.  the string, are stored.  They are packed according to the word-
  280.  length and byte-size of the particular implementation.
  281.  
  282.  Other facilities in INTCODE include directives for
  283.  initialising global variables and marking the ends of segments
  284.  of code, and a comment facility.
  285.  
  286.  We can see from this description that INTCODE is an easy
  287.  assembly language to learn and use, and that its assembler and
  288.  interpreter are simple to write.  The INTCODE kit of the BCPL
  289.  compiler consists of the source form and the corresponding
  290.  INTCODE translation of the compiler and that part of the library
  291.  that is written in BCPL.  The documentation includes a detailed
  292.  description of INTCODE and a BCPL version of the assembler and
  293.  interpreter.  To bootstrap the compiler using this kit, one
  294.  first rewrites and tests the assembler and interpreter in some
  295.  suitable language. Next, one constructs a library to provide
  296.  the necessary input and output routines.  This library consists
  297.  partly of hand-written INTCODE and partly of the compiled form
  298.  of the BCPL library suitable modified (if necessary) for the new
  299.  word-length and byte-size.  These corrections are simple as most
  300.  of this library is machine independent.  Finally, the compiler
  301.  can be assembled and tested.  To simplify this last stage, several
  302.  debugging aids are incorporated permanently into the compiler.
  303.  Many of these are in the lexical and syntax analysers which are
  304.  usually the first sections to be tested.  There is, for instance,
  305.  an option to print the current input character on every call of
  306.  the lexical analyser, and there is another option to print the
  307.  integer code of basic symbols as they are recognised.  Once the
  308.  lexical analyser works, the rest of the compiler usually works
  309.  immediately and the options to print the syntax tree and the
  310.  intermediate object code are provided mainly for their educational
  311.  value in helping implementers to understand the compiler.
  312.  
  313.  
  314.  Summary and Conclusions
  315.  -----------------------
  316.  
  317.  The OCODE mechanism provides a reasonable mechanism for
  318.  portability, since its bootstrapping capability is good and, once
  319.  the bootstrap is complete, it is possible to write (or modify) a
  320.  code-generator to compile adequately efficient code.  However, it
  321.  was found that time could be saved by using INTCODE and the
  322.  reasons for this are listed below.
  323.  
  324.  
  325.    a)  Less knowledge and less work is required to construct
  326.  the first bootstrap.
  327.  
  328.    b)  INTCODE is easier to learn and is more convenient to write
  329.  or modify than OCODE, and so it is reasonable and useful to
  330.  include many of the machine dependent parts of the library in the
  331.  kit.
  332.  
  333.    c)  Bootstrapping an INTCODE version of the BCPL compiler is
  334.  a useful educational exercise.  It allows the implementer to learn
  335.  BCPL, the specification of OCODE, and how the compiler works before
  336.  he needs to write a new code-generator.
  337.  
  338.    d)  Little of the programming of the initial bootstrap need
  339.  be discarded when the production code-generator takes over, since
  340.  much of the original code is concerned with library routines which
  341.  will still be required.  Also, it has frequently been found that
  342.  the interpretive system has sifficient advantage in size and
  343.  convenience to merit its continued existence even after the
  344.  production system is available.
  345.  
  346.    e)  The text of the INTCODE form of the compiler is more
  347.  compact than the corresponding OCODE text and this reduces the
  348.  amount of material comprising the kit.  When using magnetic tape,
  349.  this advantage is small, but when using cards or paper tape, it
  350.  is too important to ignore.
  351.  
  352.  In conclusion, INTCODE is a useful aid to simplifying the
  353.  bootstrapping problem for BCPL, but it should be remembered that
  354.  it, in itself, does not make the language portable.  For a larger
  355.  language such as Algol 68, portability is a much harder problem,
  356.  since the abstract machine is larger and more complicated,
  357.  reflecting the greater variety of data types and primitive
  358.  operations.  Furthermore, to obtain a reasonable level of
  359.  optimisation, a larger proportion of the compiler will have to
  360.  be machine dependent.  Although many of the arguments for using
  361.  an interpreter in the initial bootstrap are still valid, they
  362.  hold less weight since the scale of the job is so much larger.
  363.  Even so, the use of an interpretive scheme may prove beneficial
  364.  in some circumstances.
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  References
  373.  ----------
  374.  
  375.  
  376.  í1ó  Richards, M.      BCPL - A tool for compiler writing and
  377.  
  378.                         systems programming.
  379.                         Spring Joint Computer Conference, 1969.
  380.  
  381.  í2ó  -----------       The BCPL Programming Manual.
  382.                         The Computer Laboratory, University of
  383.                         Cambridge, 1973.
  384.  
  385.  í3ó  -----------       The Portability of the BCPL compiler.
  386.                         Software, Practice and Experience,
  387.                         Vol. 1, No. 2 (1971).
  388.  
  389.  í4ó  -----------       INTCODE - An interpretive machine code for
  390.                         BCPL.
  391.                         The Computer Laboratory, University of
  392.                         Cambridge, 1972.
  393.