Thunks - Thunks Thunks 1.1 Chapter One Chapter Overview...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Thunks Beta Draft - Do not distribute © 2001, By Randall Hyde Page 1279 Thunks Chapter One 1.1 Chapter Overview This chapter discusses thunks which are special types of procedures and procedure calls you can use to defer the e x ecution of some procedure call. Although the use of thunks is not commonplace in standard assembly code, their semantics are quite useful in AI (artifi cial intelligence) and other programs. The proper use of thunks can dramatically impro v e the performance of certain programs. Perhaps a reason thunks do not fi nd e xtensi v e use in assembly code is because most assembly language programmers are una w are of their capabilities. This chapter will solv e that problem by presenting the defi nition of thunks and describe ho w to use them in your assembly programs. 1.2 First Class Objects The actual lo w-le v el implementation of a thunk, and the in v ocation of a thunk, is rather simple. Ho w - e v er , to understand wh y you w ould w ant to use a thunk in an assembly language program we need to jump to a higher le v el of abstraction and discuss the concept of F ir st Class Objects . A fi rst class object is one you can treat lik e a normal scalar data v ariable. Y ou can pass it as a parameter (using an y arbitrary parameter passing mechanism), you can return it as a function result, you can change the object’ s v alue via certain le g al operations, you can retrie v e its v alue, and you can assign one instance of a fi rst class object to another . An int32 v ariable is a good e xample of a fi rst class object. No w consider an array . In man y languages, arrays are not fi rst class objects. Oh, you can pass them as parameters and operate on them, b ut you can’ t assign one array to another nor can you return an array as a function result in man y languages. In other languages, ho we v er , all these operations are permissible on arrays so the y are fi rst class objects (in such languages). A statement sequence (especially one in v olving procedure calls) is generally not a fi rst class object in man y programming languages. F or e xample, in C/C++ or P ascal/Delphi you cannot pass a sequence of statements as a parameter , assign them to a v ariable, return them as a function result, or otherwise operate on them as though the y were data. Y ou cannot create arbitrary arrays of statements nor can you ask a sequence of statements to e x ecute themselv es e xcept at their point of declaration. If you’ v e ne v er used a language that allo ws you to treat e x ecutable statements as data, you’ re probably w ondering wh y an yone w ould e v er w ant to do this. There are, ho we v er , some v ery good reasons for w anting to treat statements as data and e x ecute them on demand. If you’ re f amiliar with the C/C++ programming language, consider the C/C++ "?" operator: expr ? Texpr: Fexpr F or those who are unfamiliar with the "?" operator, it evaluates the first expression ( expr ) and then returns the value of Texpr if
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/08/2011 for the course CS 101 taught by Professor Jitenderkumarchhabra during the Summer '11 term at National Institute of Technology, Calicut.

Page1 / 26

Thunks - Thunks Thunks 1.1 Chapter One Chapter Overview...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online