1@c Copyright (C) 2001-2016 Free Software Foundation, Inc.
2@c This is part of the GAS manual.
3@c For copying conditions, see the file as.texinfo.
4@c MMIX description by Hans-Peter Nilsson, hp@bitrange.com
5@ifset GENERIC
6@page
7@node MMIX-Dependent
8@chapter MMIX Dependent Features
9@end ifset
10@ifclear GENERIC
11@node Machine Dependencies
12@chapter MMIX Dependent Features
13@end ifclear
14
15@cindex MMIX support
16@menu
17* MMIX-Opts::              Command-line Options
18* MMIX-Expand::            Instruction expansion
19* MMIX-Syntax::            Syntax
20* MMIX-mmixal::		   Differences to @code{mmixal} syntax and semantics
21@end menu
22
23@node MMIX-Opts
24@section Command-line Options
25
26@cindex options, MMIX
27@cindex MMIX options
28The MMIX version of @code{@value{AS}} has some machine-dependent options.
29
30@cindex @samp{--fixed-special-register-names} command line option, MMIX
31When @samp{--fixed-special-register-names} is specified, only the register
32names specified in @ref{MMIX-Regs} are recognized in the instructions
33@code{PUT} and @code{GET}.
34
35@cindex @samp{--globalize-symbols} command line option, MMIX
36You can use the @samp{--globalize-symbols} to make all symbols global.
37This option is useful when splitting up a @code{mmixal} program into
38several files.
39
40@cindex @samp{--gnu-syntax} command line option, MMIX
41The @samp{--gnu-syntax} turns off most syntax compatibility with
42@code{mmixal}.  Its usability is currently doubtful.
43
44@cindex @samp{--relax} command line option, MMIX
45The @samp{--relax} option is not fully supported, but will eventually make
46the object file prepared for linker relaxation.
47
48@cindex @samp{--no-predefined-syms} command line option, MMIX
49If you want to avoid inadvertently calling a predefined symbol and would
50rather get an error, for example when using @code{@value{AS}} with a
51compiler or other machine-generated code, specify
52@samp{--no-predefined-syms}.  This turns off built-in predefined
53definitions of all such symbols, including rounding-mode symbols, segment
54symbols, @samp{BIT} symbols, and @code{TRAP} symbols used in @code{mmix}
55``system calls''.  It also turns off predefined special-register names,
56except when used in @code{PUT} and @code{GET} instructions.
57
58@cindex @samp{--no-expand} command line option, MMIX
59By default, some instructions are expanded to fit the size of the operand
60or an external symbol (@pxref{MMIX-Expand}).  By passing
61@samp{--no-expand}, no such expansion will be done, instead causing errors
62at link time if the operand does not fit.
63
64@cindex @samp{--no-merge-gregs} command line option, MMIX
65The @code{mmixal} documentation (@pxref{mmixsite}) specifies that global
66registers allocated with the @samp{GREG} directive (@pxref{MMIX-greg}) and
67initialized to the same non-zero value, will refer to the same global
68register.  This isn't strictly enforceable in @code{@value{AS}} since the
69final addresses aren't known until link-time, but it will do an effort
70unless the @samp{--no-merge-gregs} option is specified.  (Register merging
71isn't yet implemented in @code{@value{LD}}.)
72
73@cindex @samp{-x} command line option, MMIX
74@code{@value{AS}} will warn every time it expands an instruction to fit an
75operand unless the option @samp{-x} is specified.  It is believed that
76this behaviour is more useful than just mimicking @code{mmixal}'s
77behaviour, in which instructions are only expanded if the @samp{-x} option
78is specified, and assembly fails otherwise, when an instruction needs to
79be expanded.  It needs to be kept in mind that @code{mmixal} is both an
80assembler and linker, while @code{@value{AS}} will expand instructions
81that at link stage can be contracted.  (Though linker relaxation isn't yet
82implemented in @code{@value{LD}}.)  The option @samp{-x} also imples
83@samp{--linker-allocated-gregs}.
84
85@cindex @samp{--no-pushj-stubs} command line option, MMIX
86@cindex @samp{--no-stubs} command line option, MMIX
87If instruction expansion is enabled, @code{@value{AS}} can expand a
88@samp{PUSHJ} instruction into a series of instructions.  The shortest
89expansion is to not expand it, but just mark the call as redirectable to a
90stub, which @code{@value{LD}} creates at link-time, but only if the
91original @samp{PUSHJ} instruction is found not to reach the target.  The
92stub consists of the necessary instructions to form a jump to the target.
93This happens if @code{@value{AS}} can assert that the @samp{PUSHJ}
94instruction can reach such a stub.  The option @samp{--no-pushj-stubs}
95disables this shorter expansion, and the longer series of instructions is
96then created at assembly-time.  The option @samp{--no-stubs} is a synonym,
97intended for compatibility with future releases, where generation of stubs
98for other instructions may be implemented.
99
100@cindex @samp{--linker-allocated-gregs} command line option, MMIX
101Usually a two-operand-expression (@pxref{GREG-base}) without a matching
102@samp{GREG} directive is treated as an error by @code{@value{AS}}.  When
103the option @samp{--linker-allocated-gregs} is in effect, they are instead
104passed through to the linker, which will allocate as many global registers
105as is needed.
106
107@node MMIX-Expand
108@section Instruction expansion
109
110@cindex instruction expansion, MMIX
111When @code{@value{AS}} encounters an instruction with an operand that is
112either not known or does not fit the operand size of the instruction,
113@code{@value{AS}} (and @code{@value{LD}}) will expand the instruction into
114a sequence of instructions semantically equivalent to the operand fitting
115the instruction.  Expansion will take place for the following
116instructions:
117
118@table @asis
119@item @samp{GETA}
120Expands to a sequence of four instructions: @code{SETL}, @code{INCML},
121@code{INCMH} and @code{INCH}.  The operand must be a multiple of four.
122@item Conditional branches
123A branch instruction is turned into a branch with the complemented
124condition and prediction bit over five instructions; four instructions
125setting @code{$255} to the operand value, which like with @code{GETA} must
126be a multiple of four, and a final @code{GO $255,$255,0}.
127@item @samp{PUSHJ}
128Similar to expansion for conditional branches; four instructions set
129@code{$255} to the operand value, followed by a @code{PUSHGO $255,$255,0}.
130@item @samp{JMP}
131Similar to conditional branches and @code{PUSHJ}.  The final instruction
132is @code{GO $255,$255,0}.
133@end table
134
135The linker @code{@value{LD}} is expected to shrink these expansions for
136code assembled with @samp{--relax} (though not currently implemented).
137
138@node MMIX-Syntax
139@section Syntax
140
141The assembly syntax is supposed to be upward compatible with that
142described in Sections 1.3 and 1.4 of @samp{The Art of Computer
143Programming, Volume 1}.  Draft versions of those chapters as well as other
144MMIX information is located at
145@anchor{mmixsite}@url{http://www-cs-faculty.stanford.edu/~knuth/mmix-news.html}.
146Most code examples from the mmixal package located there should work
147unmodified when assembled and linked as single files, with a few
148noteworthy exceptions (@pxref{MMIX-mmixal}).
149
150Before an instruction is emitted, the current location is aligned to the
151next four-byte boundary.  If a label is defined at the beginning of the
152line, its value will be the aligned value.
153
154In addition to the traditional hex-prefix @samp{0x}, a hexadecimal number
155can also be specified by the prefix character @samp{#}.
156
157After all operands to an MMIX instruction or directive have been
158specified, the rest of the line is ignored, treated as a comment.
159
160@menu
161* MMIX-Chars::		        Special Characters
162* MMIX-Symbols::		Symbols
163* MMIX-Regs::			Register Names
164* MMIX-Pseudos::		Assembler Directives
165@end menu
166
167@node MMIX-Chars
168@subsection Special Characters
169@cindex line comment characters, MMIX
170@cindex MMIX line comment characters
171
172The characters @samp{*} and @samp{#} are line comment characters; each
173start a comment at the beginning of a line, but only at the beginning of a
174line.  A @samp{#} prefixes a hexadecimal number if found elsewhere on a
175line.  If a @samp{#} appears at the start of a line the whole line is
176treated as a comment, but the line can also act as a logical line
177number directive (@pxref{Comments}) or a preprocessor control command
178(@pxref{Preprocessing}).
179
180Two other characters, @samp{%} and @samp{!}, each start a comment anywhere
181on the line.  Thus you can't use the @samp{modulus} and @samp{not}
182operators in expressions normally associated with these two characters.
183
184A @samp{;} is a line separator, treated as a new-line, so separate
185instructions can be specified on a single line.
186
187@node MMIX-Symbols
188@subsection Symbols
189The character @samp{:} is permitted in identifiers.  There are two
190exceptions to it being treated as any other symbol character: if a symbol
191begins with @samp{:}, it means that the symbol is in the global namespace
192and that the current prefix should not be prepended to that symbol
193(@pxref{MMIX-prefix}).  The @samp{:} is then not considered part of the
194symbol.  For a symbol in the label position (first on a line), a @samp{:}
195at the end of a symbol is silently stripped off.  A label is permitted,
196but not required, to be followed by a @samp{:}, as with many other
197assembly formats.
198
199The character @samp{@@} in an expression, is a synonym for @samp{.}, the
200current location.
201
202In addition to the common forward and backward local symbol formats
203(@pxref{Symbol Names}), they can be specified with upper-case @samp{B} and
204@samp{F}, as in @samp{8B} and @samp{9F}.  A local label defined for the
205current position is written with a @samp{H} appended to the number:
206@smallexample
2073H LDB $0,$1,2
208@end smallexample
209This and traditional local-label formats cannot be mixed: a label must be
210defined and referred to using the same format.
211
212There's a minor caveat: just as for the ordinary local symbols, the local
213symbols are translated into ordinary symbols using control characters are
214to hide the ordinal number of the symbol.  Unfortunately, these symbols
215are not translated back in error messages.  Thus you may see confusing
216error messages when local symbols are used.  Control characters
217@samp{\003} (control-C) and @samp{\004} (control-D) are used for the
218MMIX-specific local-symbol syntax.
219
220The symbol @samp{Main} is handled specially; it is always global.
221
222By defining the symbols @samp{__.MMIX.start..text} and
223@samp{__.MMIX.start..data}, the address of respectively the @samp{.text}
224and @samp{.data} segments of the final program can be defined, though when
225linking more than one object file, the code or data in the object file
226containing the symbol is not guaranteed to be start at that position; just
227the final executable.  @xref{MMIX-loc}.
228
229@node MMIX-Regs
230@subsection Register names
231@cindex register names, MMIX
232@cindex MMIX register names
233
234Local and global registers are specified as @samp{$0} to @samp{$255}.
235The recognized special register names are @samp{rJ}, @samp{rA}, @samp{rB},
236@samp{rC}, @samp{rD}, @samp{rE}, @samp{rF}, @samp{rG}, @samp{rH},
237@samp{rI}, @samp{rK}, @samp{rL}, @samp{rM}, @samp{rN}, @samp{rO},
238@samp{rP}, @samp{rQ}, @samp{rR}, @samp{rS}, @samp{rT}, @samp{rU},
239@samp{rV}, @samp{rW}, @samp{rX}, @samp{rY}, @samp{rZ}, @samp{rBB},
240@samp{rTT}, @samp{rWW}, @samp{rXX}, @samp{rYY} and @samp{rZZ}.  A leading
241@samp{:} is optional for special register names.
242
243Local and global symbols can be equated to register names and used in
244place of ordinary registers.
245
246Similarly for special registers, local and global symbols can be used.
247Also, symbols equated from numbers and constant expressions are allowed in
248place of a special register, except when either of the options
249@code{--no-predefined-syms} and @code{--fixed-special-register-names} are
250specified.  Then only the special register names above are allowed for the
251instructions having a special register operand; @code{GET} and @code{PUT}.
252
253@node MMIX-Pseudos
254@subsection Assembler Directives
255@cindex assembler directives, MMIX
256@cindex pseudo-ops, MMIX
257@cindex MMIX assembler directives
258@cindex MMIX pseudo-ops
259
260@table @code
261@item LOC
262@cindex assembler directive LOC, MMIX
263@cindex pseudo-op LOC, MMIX
264@cindex MMIX assembler directive LOC
265@cindex MMIX pseudo-op LOC
266
267@anchor{MMIX-loc}
268The @code{LOC} directive sets the current location to the value of the
269operand field, which may include changing sections.  If the operand is a
270constant, the section is set to either @code{.data} if the value is
271@code{0x2000000000000000} or larger, else it is set to @code{.text}.
272Within a section, the current location may only be changed to
273monotonically higher addresses.  A LOC expression must be a previously
274defined symbol or a ``pure'' constant.
275
276An example, which sets the label @var{prev} to the current location, and
277updates the current location to eight bytes forward:
278@smallexample
279prev LOC @@+8
280@end smallexample
281
282When a LOC has a constant as its operand, a symbol
283@code{__.MMIX.start..text} or @code{__.MMIX.start..data} is defined
284depending on the address as mentioned above.  Each such symbol is
285interpreted as special by the linker, locating the section at that
286address.  Note that if multiple files are linked, the first object file
287with that section will be mapped to that address (not necessarily the file
288with the LOC definition).
289
290@item LOCAL
291@cindex assembler directive LOCAL, MMIX
292@cindex pseudo-op LOCAL, MMIX
293@cindex MMIX assembler directive LOCAL
294@cindex MMIX pseudo-op LOCAL
295
296@anchor{MMIX-local}
297Example:
298@smallexample
299 LOCAL external_symbol
300 LOCAL 42
301 .local asymbol
302@end smallexample
303
304This directive-operation generates a link-time assertion that the operand
305does not correspond to a global register.  The operand is an expression
306that at link-time resolves to a register symbol or a number.  A number is
307treated as the register having that number.  There is one restriction on
308the use of this directive: the pseudo-directive must be placed in a
309section with contents, code or data.
310
311@item IS
312@cindex assembler directive IS, MMIX
313@cindex pseudo-op IS, MMIX
314@cindex MMIX assembler directive IS
315@cindex MMIX pseudo-op IS
316
317@anchor{MMIX-is}
318The @code{IS} directive:
319@smallexample
320asymbol IS an_expression
321@end smallexample
322sets the symbol @samp{asymbol} to @samp{an_expression}.  A symbol may not
323be set more than once using this directive.  Local labels may be set using
324this directive, for example:
325@smallexample
3265H IS @@+4
327@end smallexample
328
329@item GREG
330@cindex assembler directive GREG, MMIX
331@cindex pseudo-op GREG, MMIX
332@cindex MMIX assembler directive GREG
333@cindex MMIX pseudo-op GREG
334
335@anchor{MMIX-greg}
336This directive reserves a global register, gives it an initial value and
337optionally gives it a symbolic name.  Some examples:
338
339@smallexample
340areg GREG
341breg GREG data_value
342     GREG data_buffer
343     .greg creg, another_data_value
344@end smallexample
345
346The symbolic register name can be used in place of a (non-special)
347register.  If a value isn't provided, it defaults to zero.  Unless the
348option @samp{--no-merge-gregs} is specified, non-zero registers allocated
349with this directive may be eliminated by @code{@value{AS}}; another
350register with the same value used in its place.
351Any of the instructions
352@samp{CSWAP},
353@samp{GO},
354@samp{LDA},
355@samp{LDBU},
356@samp{LDB},
357@samp{LDHT},
358@samp{LDOU},
359@samp{LDO},
360@samp{LDSF},
361@samp{LDTU},
362@samp{LDT},
363@samp{LDUNC},
364@samp{LDVTS},
365@samp{LDWU},
366@samp{LDW},
367@samp{PREGO},
368@samp{PRELD},
369@samp{PREST},
370@samp{PUSHGO},
371@samp{STBU},
372@samp{STB},
373@samp{STCO},
374@samp{STHT},
375@samp{STOU},
376@samp{STSF},
377@samp{STTU},
378@samp{STT},
379@samp{STUNC},
380@samp{SYNCD},
381@samp{SYNCID},
382can have a value nearby @anchor{GREG-base}an initial value in place of its
383second and third operands.  Here, ``nearby'' is defined as within the
384range 0@dots{}255 from the initial value of such an allocated register.
385
386@smallexample
387buffer1 BYTE 0,0,0,0,0
388buffer2 BYTE 0,0,0,0,0
389 @dots{}
390 GREG buffer1
391 LDOU $42,buffer2
392@end smallexample
393In the example above, the @samp{Y} field of the @code{LDOUI} instruction
394(LDOU with a constant Z) will be replaced with the global register
395allocated for @samp{buffer1}, and the @samp{Z} field will have the value
3965, the offset from @samp{buffer1} to @samp{buffer2}.  The result is
397equivalent to this code:
398@smallexample
399buffer1 BYTE 0,0,0,0,0
400buffer2 BYTE 0,0,0,0,0
401 @dots{}
402tmpreg GREG buffer1
403 LDOU $42,tmpreg,(buffer2-buffer1)
404@end smallexample
405
406Global registers allocated with this directive are allocated in order
407higher-to-lower within a file.  Other than that, the exact order of
408register allocation and elimination is undefined.  For example, the order
409is undefined when more than one file with such directives are linked
410together.  With the options @samp{-x} and @samp{--linker-allocated-gregs},
411@samp{GREG} directives for two-operand cases like the one mentioned above
412can be omitted.  Sufficient global registers will then be allocated by the
413linker.
414
415@item BYTE
416@cindex assembler directive BYTE, MMIX
417@cindex pseudo-op BYTE, MMIX
418@cindex MMIX assembler directive BYTE
419@cindex MMIX pseudo-op BYTE
420
421@anchor{MMIX-byte}
422The @samp{BYTE} directive takes a series of operands separated by a comma.
423If an operand is a string (@pxref{Strings}), each character of that string
424is emitted as a byte.  Other operands must be constant expressions without
425forward references, in the range 0@dots{}255.  If you need operands having
426expressions with forward references, use @samp{.byte} (@pxref{Byte}).  An
427operand can be omitted, defaulting to a zero value.
428
429@item WYDE
430@itemx TETRA
431@itemx OCTA
432@cindex assembler directive WYDE, MMIX
433@cindex pseudo-op WYDE, MMIX
434@cindex MMIX assembler directive WYDE
435@cindex MMIX pseudo-op WYDE
436@cindex assembler directive TETRA, MMIX
437@cindex pseudo-op TETRA, MMIX
438@cindex MMIX assembler directive TETRA
439@cindex MMIX pseudo-op TETRA
440@cindex assembler directive OCTA, MMIX
441@cindex pseudo-op OCTA, MMIX
442@cindex MMIX assembler directive OCTA
443@cindex MMIX pseudo-op OCTA
444
445@anchor{MMIX-constants}
446The directives @samp{WYDE}, @samp{TETRA} and @samp{OCTA} emit constants of
447two, four and eight bytes size respectively.  Before anything else happens
448for the directive, the current location is aligned to the respective
449constant-size boundary.  If a label is defined at the beginning of the
450line, its value will be that after the alignment.  A single operand can be
451omitted, defaulting to a zero value emitted for the directive.  Operands
452can be expressed as strings (@pxref{Strings}), in which case each
453character in the string is emitted as a separate constant of the size
454indicated by the directive.
455
456@item PREFIX
457@cindex assembler directive PREFIX, MMIX
458@cindex pseudo-op PREFIX, MMIX
459@cindex MMIX assembler directive PREFIX
460@cindex MMIX pseudo-op PREFIX
461
462@anchor{MMIX-prefix}
463The @samp{PREFIX} directive sets a symbol name prefix to be prepended to
464all symbols (except local symbols, @pxref{MMIX-Symbols}), that are not
465prefixed with @samp{:}, until the next @samp{PREFIX} directive.  Such
466prefixes accumulate.  For example,
467@smallexample
468 PREFIX a
469 PREFIX b
470c IS 0
471@end smallexample
472defines a symbol @samp{abc} with the value 0.
473
474@item BSPEC
475@itemx ESPEC
476@cindex assembler directive BSPEC, MMIX
477@cindex pseudo-op BSPEC, MMIX
478@cindex MMIX assembler directive BSPEC
479@cindex MMIX pseudo-op BSPEC
480@cindex assembler directive ESPEC, MMIX
481@cindex pseudo-op ESPEC, MMIX
482@cindex MMIX assembler directive ESPEC
483@cindex MMIX pseudo-op ESPEC
484
485@anchor{MMIX-spec}
486A pair of @samp{BSPEC} and @samp{ESPEC} directives delimit a section of
487special contents (without specified semantics).  Example:
488@smallexample
489 BSPEC 42
490 TETRA 1,2,3
491 ESPEC
492@end smallexample
493The single operand to @samp{BSPEC} must be number in the range
4940@dots{}255.  The @samp{BSPEC} number 80 is used by the GNU binutils
495implementation.
496@end table
497
498@node MMIX-mmixal
499@section Differences to @code{mmixal}
500@cindex mmixal differences
501@cindex differences, mmixal
502
503The binutils @code{@value{AS}} and @code{@value{LD}} combination has a few
504differences in function compared to @code{mmixal} (@pxref{mmixsite}).
505
506The replacement of a symbol with a GREG-allocated register
507(@pxref{GREG-base}) is not handled the exactly same way in
508@code{@value{AS}} as in @code{mmixal}.  This is apparent in the
509@code{mmixal} example file @code{inout.mms}, where different registers
510with different offsets, eventually yielding the same address, are used in
511the first instruction.  This type of difference should however not affect
512the function of any program unless it has specific assumptions about the
513allocated register number.
514
515Line numbers (in the @samp{mmo} object format) are currently not
516supported.
517
518Expression operator precedence is not that of mmixal: operator precedence
519is that of the C programming language.  It's recommended to use
520parentheses to explicitly specify wanted operator precedence whenever more
521than one type of operators are used.
522
523The serialize unary operator @code{&}, the fractional division operator
524@samp{//}, the logical not operator @code{!} and the modulus operator
525@samp{%} are not available.
526
527Symbols are not global by default, unless the option
528@samp{--globalize-symbols} is passed.  Use the @samp{.global} directive to
529globalize symbols (@pxref{Global}).
530
531Operand syntax is a bit stricter with @code{@value{AS}} than
532@code{mmixal}.  For example, you can't say @code{addu 1,2,3}, instead you
533must write @code{addu $1,$2,3}.
534
535You can't LOC to a lower address than those already visited
536(i.e., ``backwards'').
537
538A LOC directive must come before any emitted code.
539
540Predefined symbols are visible as file-local symbols after use.  (In the
541ELF file, that is---the linked mmo file has no notion of a file-local
542symbol.)
543
544Some mapping of constant expressions to sections in LOC expressions is
545attempted, but that functionality is easily confused and should be avoided
546unless compatibility with @code{mmixal} is required.  A LOC expression to
547@samp{0x2000000000000000} or higher, maps to the @samp{.data} section and
548lower addresses map to the @samp{.text} section (@pxref{MMIX-loc}).
549
550The code and data areas are each contiguous.  Sparse programs with
551far-away LOC directives will take up the same amount of space as a
552contiguous program with zeros filled in the gaps between the LOC
553directives.  If you need sparse programs, you might try and get the wanted
554effect with a linker script and splitting up the code parts into sections
555(@pxref{Section}).  Assembly code for this, to be compatible with
556@code{mmixal}, would look something like:
557@smallexample
558 .if 0
559 LOC away_expression
560 .else
561 .section away,"ax"
562 .fi
563@end smallexample
564@code{@value{AS}} will not execute the LOC directive and @code{mmixal}
565ignores the lines with @code{.}.  This construct can be used generally to
566help compatibility.
567
568Symbols can't be defined twice--not even to the same value.
569
570Instruction mnemonics are recognized case-insensitive, though the
571@samp{IS} and @samp{GREG} pseudo-operations must be specified in
572upper-case characters.
573
574There's no unicode support.
575
576The following is a list of programs in @samp{mmix.tar.gz}, available at
577@url{http://www-cs-faculty.stanford.edu/~knuth/mmix-news.html}, last
578checked with the version dated 2001-08-25 (md5sum
579c393470cfc86fac040487d22d2bf0172) that assemble with @code{mmixal} but do
580not assemble with @code{@value{AS}}:
581
582@table @code
583@item silly.mms
584LOC to a previous address.
585@item sim.mms
586Redefines symbol @samp{Done}.
587@item test.mms
588Uses the serial operator @samp{&}.
589@end table
590