Title:
Aspect Oriented Programming Mechanism
Description:
It is highly desirable to have Aspect Oriented Programming mechanism
to construct reusable, flexible testbench in effective way. AOP support
mechanism is included as a part of donation to IEEE 1364 standard from
Jeda Technologies, Inc.
<AOP support in IEEE1364-2005 Jeda donation>
12. Aspect
Aspect oriented programming (AOP) is a new concept in the programming
paradigm. In this capter,
the aspect mechanism will be explained.
For the dailed concept of AOP, related information, please refere
AspectJ project web site
(www.aspectj.org).
Unlike AspectJ, (current) AOP mechanism in the testbench layer is
relatively simple. It can add
extra code called 'advice' to the location of a function call that
matches to the specified
pattern. This pattern is called pointcut. The function call is only
the target for the pointcut
pattern matching.
12.1 Aspect
The aspect block is the unit to code the aspect to Jeda code. The
aspect block is similar to the
class, but itself is the only entity and can not create the
instance of it. Thus, it can't have
'new' function. Within the aspect, member variables, member
functions, and advice can be
declared.
aspect foo_asp ;
int x, y, z ;
task foo_func ;
begin
x = 100 ;
y = 200 ;
z = 300 ;
end
endtask
advice void foo_ad ;
call( io_drive.drive_data ) ;
begin
$display( "io_drive.drive_data is called\n" ) ;
end
endadvice
endaspect
The member variables and member task/function can be referenced
with
<aspect_name>.<member_name>. In the aspect above, member variable x
can be accessed from
outside of the aspect as:
foo_asp.x = 500 ;
and member function foo_func() can be called:
foo_asp.foo_func() ;
12.2 Advice
The advice is the special executable unit for the aspect oriented
programming. Advice is similar
to member function in class that executes the code. But advice can
not be called directly from
other functions. The advice is executed at the condition specified
by the pointcut matches. The
pointcut location is defined at the other function calls. Multiple
pointcut specifications can
be declared for an advice.
aspect foo_asp ;
int x, y, z ;
advice void foo_adv
call( io_drive.drive_data ) ; // call pointcut
begin
$display( "io_drive.drive_data is called\n" ) ;
end
endadvice
endaspect
12.3 Pointcut
The pointcut is the way to pick the location and timing that the
advice is executed. There are
three mechanism to declare the pointcut specifications:
* call( <pattern> )
call pointcut is used to specify the execution of the advice at
the call (before execution)
of task/function(s) that matches the pattern.
* return( <pattern> )
return pointcut is used to specify the execution of the advice
at the return from the
function(s) that matches the pattern.
* overwrite( <pattern> )
overwrite pointcut is used to specify the execution of the
advice as the replace of the
task/function(s) that matches the pattern.
The pattern in the pointcut specification can be the exact match of
the name(s) using
identifier (e.g. io_drive.drive_data ) or use string pattern( e.g.
io_drive."drive\w*" ).
When the string is used, regular expression engine is used to
detect the matching pattern.
(See the rules in regular expression class explanation)
The pattern must be down to the function name (or string that match
to function names), but
class name and member variable names can be used to specify in the
pattern as the path down
to the function. For example, the following code catches the cycles
of the threads waiting for
the semaphore.
aspect how_long ;
int thread_count = 0 ;
int cycle_count = 0 ;
int cycle_que<> ;
advice task wait_sem() call( io_drive.drive_sem.get ) ;
begin
thread_count++ ;
cycle_que.send( get_cycle() ) ;
end
endadvice
advice task got_sem() return( io_drive.drive_sem.get ) ;
cycle_count += ( get_cycle() - cycle_que.receive() ) ;
endadvice
function int ave_wait ;
begin
if( thread_count ) return cycle_count/thread_count ;
else return 0 ;
end
endfunciton
endaspect
Note that pointcut matching is only done for functions and member
functions of classes.
Function and advice under aspect are not the target of the pointcut
search. This will prevent
the infinite recursive advice call.
12.4 Data type of advice and return value modification
When function data type is declared on advice, the type must be
matched with the type of
function(s) at the pointcut. If the type did not match, the advice
call will not be attached
to the function even the name matched to the pointcut pattern. When
the advice type is void,
the advice will be matched with any type of functions. In this
case, the return value can't be
set within the advice.
For 'call' advice case, return value on advice won't effect, as the
pointcut function will be
called after the call advice.
For 'overwrite' advice case, return value must be set by the
advice, so the advice type must be
matched with the pointcut function. The runtime error will be
detected if advice does not return
the value in this case.
For 'return' advice case, using the return statement with value
will overwrite the return value
from the pointcut function. Updating the function variable with new
value will update the return
value.
advice function int change_ret_val( int x ) return( foo.get_x ) ;
begin
if( x > 100 ) change_ret_val = 100 ; // modify the return
value
end
endadvice
For 'overwrite' advice case, the return value must be set within
the advice. If overwrite
advice does not return the value, the runtime error will be
reported as the function case.
advice function int missing_retval( int x ) overwrite( foo.get_x
) ;
int p ;
begin
p = x ;
// missing return <value> statement, will cause runtime error
end
endadvice
12.5 Arguments on advice
The advice with no argument will match with function with any
arguments. In those cases, the
advice can't see the arguments that is passed to the matched
task/function(s).
When the argument(s) are specified (which must match with that of
pointcut function), the advice
can refer the argument(s). If an argument is 'var' type, the advice
can modify it before it
passed to the actual function.
12.6 Special expressions in advice
12.6.1 this
When 'this' is used within advice, this expression returns the
instance of the top class of
the pointcut specification. When a join point 'Driver.drive' is
used, this() returns the
instance of Driver class at the pointcut. The top class name must
be deterministic to use
'this' expression within advice. The pointcut specifications must
start with a class name
(not a string), and all the class name must be the same if
multiple pointcut specifications
are specified.
advice void check_drive ;
call( Driver.drive )
call( Driver.receive) ;
Driver foo ;
begin
foo = this ;
end
endadvice
12.6.1 return
This special expression returns the return value of the join
point function. Only valid when
the advice type is specified, and it only can be used in return
pointcut advice. When this
expression is used in non-advice block, it will cause a compile
error. When this expression
is used in non-return pointcut advice, it will cause a runtime
error. The data type of the
return expression is the same as the advice data type.
advice int add_one return( foo.bar ) ;
int rval ;
begin
rval = return ;
rval++ ;
add_one = rval ;
end
endadvice
12.7 Advice Related Functions
There are two special system functions that can be used within
advice.
* function int pointcut_line()
This function returns the current line number of the pointcut
location.
* function string pointcut_file()
This function returns the file name of the pointcut location.
These are handy to add some debugging information as an aspect. The
following example add
extra line and file printout to any printf call within the Driver
class.
aspect verbose ;
advice task verbose_display call( Driver.".*".$display )
begin
$write( "Line %d in %s", pointcut_line(), pointcut_file() ) ;
end
endadvice
endaspect
Received on Tue Aug 31 00:54:44 2004
This archive was generated by hypermail 2.1.8 : Tue Aug 31 2004 - 00:54:51 PDT