This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Machine Description File help


> Date: Mon, 8 Jul 2002 11:10:07 +0100 (BST)
> From: =?iso-8859-1?q?yoshi=20ge?= <moriandiz@yahoo.co.in>
> To: gcc@gcc.gnu.org

> Anybody is there to help me to understand the following from
> i386.md. Please don't tell me to read online doc. To understand that
> I must do a lot of hard work. If anybody explains me the following I
> will be able to understand the online docs.

If would be useful for you to say what you don't understand about it,
so that people can tailor their explanation to cover those things that
you don't understand and avoiding those things that you do.  For
example, if you have never seen anything like lisp, it will be harder
to understand the code, but if you're a 10 year lisp veteran certain
parts of the code will be perfectly clear to you.

I will error on the side of, you are unfamiliar with lisp.  If you
would like to be able to read a book that covers this in detail, I
think you'd find value in a lisp book.  It would cover the basics like
why there are [] and () everywhere, and why cond has a value, and what
the semantics of cond are, and why eq_attr comes first in the () list,
and so on...

> /****************************************************
> (define_attr "memory" "none,load,store,both,unknown"

This defines an attribute called memory, with 5 symbolic enumeration
constants which are none, load, store, both and unknown.  From the
manual:

@findex define_attr
The @code{define_attr} expression is used to define each attribute required
by the target machine.  It looks like:

@smallexample
(define_attr @var{name} @var{list-of-values} @var{default})
@end smallexample

@var{name} is a string specifying the name of the attribute being defined.

@var{list-of-values} is either a string that specifies a comma-separated
list of values that can be assigned to the attribute, or a null string to
indicate that the attribute takes numeric values.

@var{default} is an attribute expression that gives the value of this
attribute for insns that match patterns whose definition does not include
an explicit value for this attribute.  @xref{Attr Example}, for more
information on the handling of defaults.  @xref{Constant Attributes},
for information on attributes that do not depend on any particular insn.

@findex insn-attr.h
For each defined attribute, a number of definitions are written to the
@file{insn-attr.h} file.  For cases where an explicit set of values is
specified for an attribute, the following are defined:

@itemize @bullet
@item
A @samp{#define} is written for the symbol @samp{HAVE_ATTR_@var{name}}.

@item
An enumeral class is defined for @samp{attr_@var{name}} with
elements of the form @samp{@var{upper-name}_@var{upper-value}} where
the attribute name and value are first converted to upper case.

@item
A function @samp{get_attr_@var{name}} is defined that is passed an insn and
returns the attribute value for that insn.
@end itemize

For example, if the following is present in the @file{md} file:

@smallexample
(define_attr "type" "branch,fp,load,store,arith" @dots{})
@end smallexample

@noindent
the following lines will be written to the file @file{insn-attr.h}.

@smallexample
#define HAVE_ATTR_type
enum attr_type @{TYPE_BRANCH, TYPE_FP, TYPE_LOAD,
                 TYPE_STORE, TYPE_ARITH@};
extern enum attr_type get_attr_type ();
@end smallexample

If the attribute takes numeric values, no @code{enum} type will be
defined and the function to obtain the attribute's value will return
@code{int}.

>   (cond [

cond means there are a group of condition and actions that follow, from the manual:

@findex cond
@item (cond [@var{test1} @var{value1} @var{test2} @var{value2} @dots{}] @var{default})
Similar to @code{if_then_else}, but more general.  Each of @var{test1},
@var{test2}, @dots{} is performed in turn.  The result of this expression is
the @var{value} corresponding to the first nonzero test, or @var{default} if
none of the tests are nonzero expressions.

This is currently not valid for instruction patterns and is supported only
for insn attributes.  @xref{Insn Attributes}.
@end table

>         (eq_attr "type" "other,multi,str")

This is a condition that checks for equality of an attribute, the
operands for the equality check are an attribute called type, and the 
set (other, multi and str).  From the manual:

@findex eq_attr
@item (eq_attr @var{name} @var{value})
@var{name} is a string specifying the name of an attribute.

@var{value} is a string that is either a valid value for attribute
@var{name}, a comma-separated list of values, or @samp{!} followed by a
value or list.  If @var{value} does not begin with a @samp{!}, this
test is true if the value of the @var{name} attribute of the current
insn is in the list specified by @var{value}.  If @var{value} begins
with a @samp{!}, this test is true if the attribute's value is
@emph{not} in the specified list.

For example,

@smallexample
(eq_attr "type" "load,store")
@end smallexample

@noindent
is equivalent to

@smallexample
(ior (eq_attr "type" "load") (eq_attr "type" "store"))
@end smallexample

If @var{name} specifies an attribute of @samp{alternative}, it refers to the
value of the compiler variable @code{which_alternative}
(@pxref{Output Statement}) and the values must be small integers.  For
example,

@smallexample
(eq_attr "alternative" "2,3")
@end smallexample

@noindent
is equivalent to

@smallexample
(ior (eq (symbol_ref "which_alternative") (const_int 2))
     (eq (symbol_ref "which_alternative") (const_int 3)))
@end smallexample

Note that, for most attributes, an @code{eq_attr} test is simplified in cases
where the value of the attribute being tested is known for all insns matching
a particular pattern.  This is by far the most common case.

> 	  (const_string "unknown")

The `value' of the `cond' expression and hence the entire attribute
called memory is the constant string "unknown" if the above condition
is true...

> 	 (eq_attr "type" "lea,fcmov,fpspc,cld")

otherwise we check to see if type is ones of those values, and if it is, then

> 	   (const_string "none")

"none" is the answer...

> 	 (eq_attr "type" "push")

otherwise if the instruction was a push

> 	   (if_then_else (match_operand 1 "memory_operand" "")

and if operand 1 is a memory_operand, then

> 	     (const_string "both")

the answer is "both", otherwise

> 	     (const_string "store"))

we know that the instruction was still a push and operand 1 is not a
memory_operand and the answer is "store".

> 	 (eq_attr "type" "pop,setcc")

If it wasn't a push, but rather a pop or setcc, then

> 	   (if_then_else (match_operand 0 "memory_operand"
> "")
> 	     (const_string "both")
> 	     (const_string "load"))

...


In C, it would be coded:

enum { NONE, LOAD, STORE, BOTH, UNKNOWN }
memory () {
       switch (type()) {
       case OTHER:
       case MULTI:
       case STR:
	    return UNKNOWN;
       }
       switch (type()) {
       case LEA:
       case FCMOV:
       case FPSPC:
       case CLD:
	    return NONE;
       }
       switch (type()) {
       case PUSH:
	    if (match_operand (1, "memory_operand", ""))
	       return BOTH;
	    else
	       return STORE;
       }
       switch (type()) {
       case POP:
       case SETCC:
	    ...
       }
       ...
}

You can also know what it does by reading the comment just before it:

;; The `memory' attribute is `none' if no memory is referenced, `load' or
;; `store' if there is a simple memory reference therein, or `unknown'
;; if the instruction is complex.

If something is still unclear, I'd be happy to try and help you
understand in more detail, but please focus in on one aspect that is
unclear, say by listing possible things you think it could mean, and
asking which one, if any of those semantics the code represents.  If
the `whole thing' is unclear or if the manual is unclear, then I would
recommend focusing on more of the basics.  This list isn't really the
best place to try and get up to speed on the complexities of
compilers, lisp and the like.

Hope that helps you get started...


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]