[m-rev.] for review: x86-64 instructions and output files

Julien Fischer juliensf at csse.unimelb.edu.au
Mon Jan 15 14:28:27 AEDT 2007


On Mon, 15 Jan 2007, Fransiska Nathania HANDOKO wrote:

> Estimated hours taken: 50
> Branches: main
>
> Create a file to represent x86_64 instruction sets and another file to print 
> the instructions out for the needs to compile LLDS to x86_64.
>
> compiler/x86_64_instrs.m:
>        Defines x86_64 instructions using AT&T syntax.

It defines a representation of (a subset of) the x86_64 instruction set.
It doesn't really have much to do with the AT&T syntax aside from the
fact that we use the same operand ordering, i.e. src to dst.  (And of
course that we write the instructions out in AT&T syntax).

> compiler/x86_64_out.m:
>        Printing out x86_64 instructions.

I suggest:
 	Predicates to write out x86_64 instructions.

...

> Index: compiler/ll_backend.m
> ===================================================================
> RCS file: /home/mercury/mercury1/repository/mercury/compiler/ll_backend.m,v
> retrieving revision 1.17
> diff -u -r1.17 ll_backend.m
> --- compiler/ll_backend.m        27 Jul 2006 05:01:10 -0000        1.17
> +++ compiler/ll_backend.m        8 Jan 2007 03:15:58 -0000
> @@ -92,6 +92,10 @@
> :- include_module layout_out.
> :- include_module rtti_out.
>
> +% The LLDS->x86_64 asm phase.
> +:- include_module x86_64_instrs.
> +:- include_module x86_64_out.
> +
> :- implementation.
>
> :- import_module check_hlds.        % needed for type_util, mode_util etc

This change is not mentioned in the log message.

> Index: compiler/mercury_compile.m
> ===================================================================
> RCS file:
> /home/mercury/mercury1/repository/mercury/compiler/mercury_compile.m,v
> retrieving revision 1.423
> diff -u -r1.423 mercury_compile.m
> --- compiler/mercury_compile.m        10 Jan 2007 02:49:21 -0000 1.423
> +++ compiler/mercury_compile.m        12 Jan 2007 04:26:08 -0000
> @@ -3651,7 +3651,7 @@
>
> maybe_tuple_arguments(Verbose, Stats, !HLDS, !IO) :-
>     module_info_get_globals(!.HLDS, Globals),
> -    globals.lookup_bool_option(Globals, tuple, Tuple),
> +    globals.lookup_bool_option(Globals, untuple, Tuple),
>     (
>         Tuple = yes,
>         maybe_write_string(Verbose, "% Tupling...\n", !IO),

Please don' commit this bit, it's wrong.  It appears to be the result
of a bad CVS merge.

> Index: compiler/x86_64_instrs.m
> ===================================================================
> RCS file: compiler/x86_64_instrs.m
> diff -N compiler/x86_64_instrs.m
> --- /dev/null        1 Jan 1970 00:00:00 -0000
> +++ compiler/x86_64_instrs.m        12 Jan 2007 04:29:30 -0000
> @@ -0,0 +1,1629 @@
> +%-----------------------------------------------------------------------------%
> +% vim: ft=mercury ts=4 sw=4 et
> +%-----------------------------------------------------------------------------%
> +% Copyright (C) 2007 The University of Melbourne.
> +% This file may only be copied under the terms of the GNU General
> +% Public License - see the file COPYING in the Mercury distribution.
> +%-----------------------------------------------------------------------------%
> +%
> +% File: x86_64_instrs.m.
> +% Main author: fhandoko.
> +%
> +% This module contains the representations of the x86_64 instructions.
> +%
> +%-----------------------------------------------------------------------------%
> +
> +:- module ll_backend.x86_64_instrs.
> +:- interface.
> +
> +:- import_module list.
> +:- import_module maybe.
> +
> +%-----------------------------------------------------------------------------%
> +
> +:- type x86_64_instruction
> +    --->    x86_64_instr(
> +                x86_64_instr_name   :: x86_64_op,
> +                x86_64_comment      :: string
> +            ).
> +
> +:- type label_name == string.
> +
> +:- type x86_64_instr
> +    --->    comment(string)
> +    ;       label(label_name)
> +    ;       directives(list(pseudo_op))
> +    ;       instrs(list(x86_64_instruction)).
> +
> +%-----------------------------------------------------------------------------%
> +
> +    % Signed integers of various sizes.
> +    %
> +:- type int8 ---> int8(int).        % In bottom 8 bits.
> +:- type int16 ---> int16(int).      % In bottom 16 bits.
> +:- type int32 ---> int32(int).      % in bottom 32 bits.
> +
> +    % Unsigned integers of various sizes.
> +    %
> +:- type uint8 ---> uint8(int).      % In bottom 8 bits.
> +:- type uint16 ---> uint16(int).    % In bottom 16 bits.
> +:- type uint32 ---> uint32(int).    % In bottom 32 bits.
> +
> +
> +%-----------------------------------------------------------------------------%
> +%
> +% x86_64 pseudo-ops.
> +%
> +
> +    % Pseudo op for x86_64. Also called assembler directive.
> +    %

I suggest:

 	GNU assembler pseudo-ops for the x86_64.

You should add a reference to the appropriate section of the as manual.

...

> +%-----------------------------------------------------------------------------%
> +%
> +% x86_64 instructions.
> +%
> +

I didn't bother looking at all the instructions in detail.


> Index: compiler/x86_64_out.m
> ===================================================================
> RCS file: compiler/x86_64_out.m
> diff -N compiler/x86_64_out.m
> --- /dev/null        1 Jan 1970 00:00:00 -0000
> +++ compiler/x86_64_out.m        12 Jan 2007 04:29:28 -0000
> @@ -0,0 +1,1311 @@

...

Add a comment mentioning that pretend_main/2 is just there for testing
purposes.

> +% pretend_main(!IO) :-
> +%     Comment1 = comment("This is a comment"),
> +%     Label1 = label(".L1"),
> +%     PseudoOps1 = [
> +%         abort,
> +%         align(6, no, yes(2)),
> +%         align(7, yes(12), no),
> +%         align(8, no, no),
> +%         ascii([".LI", ".L2"]),
> +%         comm(".L3", 8, no),
> +%         section(".data", no, no, no)
> +%         ],
> +%     Instrs1 = [
> +%         x86_64_instr(adc(operand_reg(rax),
> +%                      rmro_reg(rbx)
> +%                     ), ""),
> +%         x86_64_instr(add(operand_imm(imm8(int8(0x178))),
> +%                      rmro_mem_ref(mem_abs(base_reg(0, r8)))
> +%                     ), ""),
> +%         x86_64_instr(and(operand_reg(rdx),
> +%                      rmro_mem_ref(mem_abs(base_reg(2, rdx)))
> +%                     ), ""),
> +%         x86_64_instr(bsf(rmro_reg(r8), r15), ""),
> +%         x86_64_instr(bsr(rmro_reg(rcx), rax), ""),
> +%         x86_64_instr(bswap(r10), ""),
> +%         x86_64_instr(bt(rmro_mem_ref(mem_rip(rip_expr(".L1"))),
> +%             rio_reg(rsi)), ""),
> +%         x86_64_instr(btc(rmro_reg(rdi), 
> rio_imm(imm16(int16(0x1822)))),""),
> +%         x86_64_instr(btr(rmro_reg(rbp),
> +%             rio_imm(imm32(int32(0x182199)))), ""),
> +%         x86_64_instr(call(rmrol_rel_offset(ro8(int8(127)))), "comment"),
> +%         x86_64_instr(cmovo(rmro_mem_ref(mem_rip(rip_constant(int32(2)))),
> +%             r11), ""),
> +%         x86_64_instr(mov(operand_imm(imm32(int32(10))), 
> rmro_reg(rbx)),""),
> +%         x86_64_instr(or(operand_mem_ref(mem_rip(rip_constant(int32(2)))),
> +%            rmro_reg(rcx)), ""),
> +%         x86_64_instr(push(operand_mem_ref(mem_abs(base_expr(".L2")))),""),
> +%          x86_64_instr(rol(crio_reg_cl(rcx), rmro_reg(rax)), ""),
> +%         x86_64_instr(ror(crio_imm8(int8(20)), rmro_mem_ref(mem_rip(
> +%             rip_expr(".L2")) )), ""),
> +%         x86_64_instr(sbb(operand_mem_ref(mem_rip(rip_expr(".L1"))),
> +%             rmro_reg(r11)), "")
> +%         ],
> +%
> +%     Label2 = label(".L2"),
> +%     PseudoOps2 = [
> +%         section(".rodata", yes("aMS"), yes("@progbits"), yes(1)),
> +%         p2align(1,no,yes(7)),
> +%         type_("Type", "@function")
> +%         ],
> +%     Instrs2 = [
> +%         x86_64_instr(loop(ro8(int8(19))), "another comment"),
> +%         x86_64_instr(xor(operand_imm(imm32(int32(19))),
> +%                      rmro_mem_ref(mem_rip(rip_expr(".L4")))
> +%                     ), "comment"),
> +%         x86_64_instr(jo(ro8(int8(22))), "comment again"),
> +%         x86_64_instr(div(rmro_reg(rdx)), "comment div"),
> +%         x86_64_instr(jmp(rmrol_label(".L2")), "comment jmp"),
> +%         x86_64_instr(mov(operand_mem_ref(mem_abs(base_expr(".L3"))),
> +%             rmro_reg(rbx)), "")
> +%         ],
> +%
> +%     Instructions = [
> +%         comment(""), label(""), directives([file("x86_64_out.m")]),
> +%           instrs([]),
> +%         Comment1, Label1, directives(PseudoOps1), instrs(Instrs1),
> +%         comment("Comment"), Label2, directives(PseudoOps2),
> +%           instrs(Instrs2)
> +%         ],
> +%     list.foldl(output_x86_64_instrs, Instructions, !IO).

Two comments on the remaining code:

(1) the output predicates should have an argument representing the
output stream threaded through them.

(2) Many of the if-then-elses can be replaced by switches (especially
those involving the types bool/0 and maybe/1 - I think we discussed this
in person.)

Both of the above changes can be made separately.

Since this change shouldn't affect anything else you can go ahead and
commit it (after bootchecking it.)

Julien.
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list