Embedded System Design

The Washington Institute for Near East Policy

Politico.com: Politics '08

Monday, August 13, 2007

The basics of programming embedded processors: Part 4

By Wayne Wolf , Embedded.com
Aug 13 2007 (3:00 AM)
URL: http://www.embedded.com/showArticle.jhtml?articleID=201500002

Another major code generation problem is the creation of procedures. Generating code for procedures is relatively straightforward once we know the procedure linkage appropriate for the CPU. At the procedure definition, we generate the code to handle the procedure call and return. At each call of the procedure, we set up the procedure parameters and make the call.

The CPU's subroutine call mechanism is usually not sufficient to directly support procedures in modern programming languages. The procedure linkage mechanism provides a way for the program to pass parameters into the program and for the procedure to return a value.


It also provides help in restoring the values of registers that the procedure has modified. All procedures in a given programming language use the same linkage mechanism (although different languages may use different linkages). The mechanism can also be used to call handwritten assembly language routines from compiled code.

Procedure stacks are typically built to grow down from high addresses. A stack pointer (sp) defines the end of the current frame, while a frame pointer (fp) defines the end of the last frame. (The fp is technically necessary only if the stack frame can be grown by the procedure during execution.)


The procedure can refer to an element in the frame by addressing relative to sp. When a new procedure is called, the sp and fp are modified to push another frame onto the stack.

The ARM Procedure Call Standard (APCS) is a good illustration of a typical procedure linkage mechanism. Although the stack frames are in main memory, understanding how registers are used is key to understanding the mechanism, as explained below.

r0"r3 are used to pass parameters into the procedure. r0 is also used to hold the return value. If more than four parameters are required, they are put on the stack frame.
r4"r7 hold register variables.
r11 is the frame pointer and r13 is the stack pointer.
r10 holds the limiting address on stack size, which is used to check for stack overflows.
Other registers have additional uses in the protocol.



Figure 5-14. Layout of a one-dimensional array in memory

Data Structures
The compiler must also translate references to data structures into references to raw memories. In general, this requires address computations. Some of these computations can be done at compile time while others must be done at run time. arrays Arrays are interesting because the address of an array element must in general be computed at run time, since the array index may change. Let us first consider one-dimensional arrays:

a[i]

The layout of the array in memory is shown in Figure 5-14 above. The zeroth element is stored as the first element of the array, the first element directly below, and so on. We can create a pointer for the array that points to the array's head, namely, a[0]. If we call that pointer aptr for convenience, then we can rewrite the reading of a [i] as

*(aptr + i)

Two-dimensional arrays are more challenging. There are multiple possible ways to lay out a two-dimensional array in memory, as shown in Figure 5-15 below. In this form, which is known as row major, the inner variable of the array (j in a[i,j]) varies most quickly. (Fortran uses a different organization known as column major.)


Two-dimensional arrays also require more sophisticated addressing - in particular, we must know the size of the array. Let us consider the row-major form. If the a[] array is of size N × M, then we can turn the two-dimensional array access into a one-dimensional array access. Thus,

a[i,j]

becomes

a[i*M + j]

where the maximum value for j is M ' 1.



Figure 5-15. Memory layout for two-dimensional arrays

A C struct is easier to address. As shown in Figure 5-16 below, a structure is implemented as a contiguous block of memory. Fields in the structure can be accessed using constant offsets to the base address of the structure. In this example, if field1 is four bytes long, then field2 can be accessed as

*(aptr + 4)

This addition can usually be done at compile time, requiring only the indirection itself to fetch the memory location during execution.

Expression Simplification
Expression simplification is a useful area for machine-independent transformations. We can use the laws of algebra to simplify expressions. Consider the following expression:

a*b + a*c

We can use the distributive law to rewrite the expression as

a*(b + c)


Figure 5-16. C structure layout and access.

Since the new expression has only two operations rather than three for the original form, it is almost certainly cheaper, because it is both faster and smaller. Such transformations make some broad assumptions about the relative cost of operations.


In some cases, simple generalizations about the cost of operations may be misleading. For example, a CPU with a multiply-andaccumulate instruction may be able to do a multiply and addition as cheaply as it can do an addition. However, such situations can often be taken care of in code generation.

We can also use the laws of arithmetic to further simplify expressions on constants. Consider the following C statement:

for (i = 0; i < 8 + 1; i++)

We can simplify 8 + 1 to 9 at compile time - there is no need to perform that arithmetic while the program is executing. Why would a program ever contain expressions that evaluate to constants? Using named constants rather than numbers is good programming practice and often leads to constant expression. The original form of the for statement could have been

for (i = 0; i < NOPS + 1; i++)

where, for example, the added 1 takes care of a trailing null character.


Dead Code Elimination
Code that will never be executed can be safely removed from the program. The general problem of identifying code that will never be executed is difficult, but there are some important special cases where it can be done. Programmers will intentionally introduce dead code in certain situations.


Consider this C code fragment:


#define DEBUG 0 ... if (DEBUG) print_debug_stuff();


In the above case, the print_debug_stuff() function is never executed, but the code allows the programmer to override the preprocessor variable definition (perhaps with a compile-time flag) to enable the debugging code.


This case is easy to analyze because the condition is the constant 0, which C uses for the false condition. Since there is no else clause in the if statement, the compiler can totally eliminate the if statement, rewriting the CDFG to provide a direct edge between the statements before and after the if.

Some dead code may be introduced by the compiler. For example, certain optimizations introduce copy statements that copy one variable to another. If uses of the first variable can be replaced by references to the second one, then the copy statement becomes dead code that can be eliminated.

Procedure Inlining
Another machine-independent transformation that requires a little more evaluation is procedure inlining. An inlined procedure does not have a separate procedure body and procedure linkage; rather, the body of the procedure is substituted in place for the procedure call. Figure 5-17 below shows an example of function inlining in C.


The C++ programming language provides an inline construct that tells the compiler to generate inline code for a function. In this case, an inlined procedure is generated in expanded form whenever possible. However, inlining is not always the best thing to do. It does eliminate the procedure linkage instructions. However, in a cached system, having multiple copies of the function body may actually slow down the fetches of these instructions. Inlining also increases code size, and memory may be precious.

Function definition:
int foo(a,b,c) { return a + b " c; }

Function call:
z = foo(w,x,y,);

Inlining result:
z = w + x " y;

Figure 5-17. Function inlining in C.

Loop Transformations
Loops are important program structures - although they are compactly described in the source code, they often use a large fraction of the computation time. Many techniques have been designed to optimize loops.

A simple but useful transformation is known as loop unrolling, which is illustrated in Example 5-4 below. Loop unrolling is important because it helps expose parallelism that can be used by later stages of the compiler.

Example 5-4 Loop Unrolling
A simple loop in C follows:

for (i = 0; i < N; i++) { a[i]=b[i]*c[i]; }
This loop is executed a fixed number of times, namely, N. A straightforward implementation of the loop would create and initialize the loop variable i, update its value on every iteration, and test it to see whether to exit the loop. However, since the loop is executed a fixed number of times, we can generate more direct code.

If we let N = 4, then we can substitute the above C code for the following loop:

a[0] = b[0]*c[0]; a[1] = b[1]*c[1]; a[2] = b[2]*c[2]; a[3] = b[3]*c[3];
This unrolled code has no loop overhead code at all, that is, no iteration variable and no tests. But the unrolled loop has the same problems as the inlined procedure—it may interfere with the cache and expands the amount of code required.

We do not, of course, have to fully unroll loops. Rather than unroll the above loop four times, we could unroll it twice. The following code results:

for (i = 0; i < 2; i++) { a[i*2] = b[i*2]*c[i*2]; a[i*2 + 1] = b[i*2 + 1]*c[i*2 + 1]; }
In this case, since all operations in the two lines of the loop body are independent, later stages of the compiler may be able to generate code that allows them to be executed efficiently on the CPU's pipeline.

Loop fusion combines two or more loops into a single loop. For this transformation to be legal, two conditions must be satisfied. First, the loops must iterate over the same values. Second, the loop bodies must not have dependencies that would be violated if they are executed together - for example, if the second loop's ith iteration depends on the results of the i + 1th iteration of the first loop, the two loops cannot be combined. Loop distribution is the opposite of loop fusion, that is, decomposing a single loop into multiple loops.

Loop tiling breaks up a loop into a set of nested loops, with each inner loop performing the operations on a subset of the data. An example is shown in Figure 5-18 below. Here, each loop is broken up into tiles of size two. Each loop is split into two loops - for example, the inner ii loop iterates within the tile and the outer i loop iterates across the tiles.


The result is that the pattern of accesses across the a array is drastically different - rather than walking across one row in its entirety, the code walks through rows and columns following the tile structure. Loop tiling changes the order in which array elements are accessed, thereby allowing us to better control the behavior of the cache during loop execution.


Figure 5-18. Loop tiling.

We can also modify the arrays being indexed in loops. Array padding adds dummy data elements to a loop in order to change the layout of the array in the cache. Although these array locations will not be used, they do change how the useful array elements fall into cache lines. Judicious padding can in some cases significantly reduce the number of cache conflicts during loop execution.

Next in Part 5: Register allocation and scheduling
To read Part 1, go to "Program design and analysis."
To read Part 2 , go to " Models of program, assemblers and linkers."
To read Part 3, go to "Basic compilation techniques"

Used with the permission of the publisher, Newnes/Elsevier, this series of six articles is based on copyrighted material from "Computers as Components: Principles of Embedded Computer System Design" by Wayne Wolf. The book can be purchased on line.

Wayne Wolf is currently the Georgia Research Alliance Eminent Scholar holding the Rhesa "Ray" S. Farmer, Jr., Distinguished Chair in Embedded Computer Systems at Georgia Tech's School of Electrical and Computer Engineering (ECE). Previously a professor of electrical engineering at Princeton University, he worked at AT&T Bell Laboratories. He has served as editor in chief of the ACM Transactions on Embedded Computing and of Design Automation for Embedded Systems.

16 comments:

Anonymous said...

delaware ykhkkak rebuttal airplanes dancing ktud zensar arguedas hepatoxicity basicomega aman
lolikneri havaqatsu

Anonymous said...

music mates dating [url=http://loveepicentre.com/]direct dating london[/url] love dating sim for girls cheats http://loveepicentre.com/ totally free online dating

Anonymous said...

compare laptops [url=http://www.hqlaptopbatteries.com/-s5-laptopbatterymodel883.html]notebook batteries[/url] dell laptop http://www.hqlaptopbatteries.com/battery-4100wlmi-batterytype1.html Compaq laptop battery
Averatec Laptop [url=http://www.hqlaptopbatteries.com/-p20-108-laptopbatterymodel1885.html]sony laptop[/url] laptop batteries reviews http://www.hqlaptopbatteries.com/gateway-nx-laptopbatterymodel645.html Professional Laptop Battery Wholesale Site
Lenovo Laptop [url=http://www.hqlaptopbatteries.com/battery-5633wlmi-batterytype1.html]Discount Laptop Batteries[/url] IBM laptop battery http://www.hqlaptopbatteries.com/-3000lm-laptopbatterymodel1347.html Find a Dell Laptop Battery

Anonymous said...

http://meen.in/cleocin/cleocin-cream
[url=http://meen.in/erections/tobasco-sauce-and-erections]orange peel as a drug[/url] best natural viagra [url=http://meen.in/cetirizine/cetirizine-vs-loratadine]cetirizine vs loratadine[/url]
drugs that mood elevators http://meen.in/carbohydrates/pictures-of-foods-that-have-carbohydrates
[url=http://meen.in/fda/jan-marini-fda]colonial drugs orlando fl[/url] what is a tier 1 drug [url=http://meen.in/fluoxetine/purchase-fluoxetine-online]purchase fluoxetine online[/url]
drugs judith bryce http://meen.in/flutamide/flutamide-tablet
[url=http://meen.in/celebrex/celebrex-side-effect]but viagra[/url] how do u use club drugs [url=http://meen.in/carisoprodol/can-you-take-asacol-and-carisoprodol-at-the-same-time]can you take asacol and carisoprodol at the same time[/url] buy cialis in south africa online [url=http://meen.in/carisoprodol/carisoprodol-cheap-soma-100]carisoprodol cheap soma 100[/url]

Anonymous said...

designer dress vancouver wedding http://topcitystyle.com/armani-jeans-pullover-for-men-navy-blue-item2250.html mephisto shoes retail stores [url=http://topcitystyle.com/grey-gucci-color1.html]cool boys clothes[/url] atlee fashion photographer simon
http://topcitystyle.com/-casual-roberto-cavalli-category30.html bernina designer plus extra fills [url=http://topcitystyle.com/emporia-armani-jeans-for-women-denim-blue-item2347.html]ralph lauren loura[/url]

Anonymous said...

graphic designer job opportunities http://topcitystyle.com/?action=products&product_id=2163 boston fashion designer [url=http://topcitystyle.com/46-leather-shoes-size4.html]the clothes rack for kids[/url] formal shoes
http://topcitystyle.com/takeshy-kurosawa-t-shirt-for-men-white-item2031.html chanel replica sunglasses [url=http://topcitystyle.com/jeans-armani-category14.html]exotic shoes[/url]

Anonymous said...

best travel route http://xwa.in/hotel/hotel-valadonparis discount international travel olympic airlines egypt airlines
[url=http://xwa.in/map/road-map-of-wiltshire]train travel sites uk[/url] global travel international travel agent [url=http://xwa.in/tourism/djibouti-tourism]djibouti tourism[/url]
cathedral of faith travel http://xwa.in/inn/holiday-inn-in-mission-valley
[url=http://xwa.in/adventure/kayak-tour-rental-adventure-business-for-sale]top 10 travel trailers[/url] stroller travel systems [url=http://xwa.in/tourist/york-tourist-information]york tourist information[/url]
webcrawler mac travel http://xwa.in/adventure/pikemen-marland-dimond-adventure damar travel [url=http://xwa.in/cruises/carnival-caribbean-cruises]carnival caribbean cruises[/url]

Anonymous said...

crampy with vaginal mucous [url=http://usadrugstoretoday.com/products/vasotec.htm]vasotec[/url] free gay muscle hunks http://usadrugstoretoday.com/categories/sleeping-aid.htm
liver disorder and bright red blood in stool [url=http://usadrugstoretoday.com/catalogue/l.htm]Buy generic and brand medications[/url] department of public health medication errors [url=http://usadrugstoretoday.com/catalogue/j.htm ]weight loss centers in tulsa oklahoma [/url] aspirin containing additives
the largest penis pics [url=http://usadrugstoretoday.com/products/yasmin.htm]yasmin[/url] hospital quality breast pumps http://usadrugstoretoday.com/products/acai.htm
members health [url=http://usadrugstoretoday.com/categories/anti-herpes.htm]anti herpes[/url] hypertension and blood pressure [url=http://usadrugstoretoday.com/index.php?lng=es&cv=eu ]arterial stenosis heart [/url] honey and staph infection

Anonymous said...

all about calcium [url=http://usadrugstoretoday.com/products/retin-a-0-05-.htm]Buy Retin-A 0.05% Low prices, side effects, interactions[/url] medicine like lexapro http://usadrugstoretoday.com/products/metoclopramide.htm
women smoking cigars naked [url=http://usadrugstoretoday.com/categories/sante-generale.htm]sante generale[/url] drug reference white tablet 547 [url=http://usadrugstoretoday.com/categories/huesos-sanos.htm ]drug treatment courts an integrated approach class [/url] orlando regional medical center
free use of breast cancer ribbon [url=http://usadrugstoretoday.com/products/cytoxan.htm]cytoxan[/url] symptoms of parental alienation syndrome http://usadrugstoretoday.com/products/provera.htm
bombay tea box [url=http://usadrugstoretoday.com/products/avodart.htm]avodart[/url] best acne antibiotics [url=http://usadrugstoretoday.com/products/zyvox.htm ]provo breast enhancement [/url] tea medical abbreviation carotid endarterectomy

Anonymous said...

medical abbreviation inr [url=http://usadrugstoretoday.com/catalogue/x.htm]medications without a prescription[/url] difference between viagra and levitra http://usadrugstoretoday.com/products/ponstel.htm
medline drug information [url=http://usadrugstoretoday.com/categories/controllo-delle-nascite.htm]controllo delle nascite[/url] hypertension and heart damage [url=http://usadrugstoretoday.com/products/requip.htm ]hippa non medical work [/url] no egg diet
cytomel calcium [url=http://usadrugstoretoday.com/categories/erection-packs.htm]erection packs[/url] make my penis bigger http://usadrugstoretoday.com/products/voltaren.htm
canine metronidazole dosage [url=http://usadrugstoretoday.com/products/lasix.htm]lasix[/url] sacred heart minnesota [url=http://usadrugstoretoday.com/products/prednisone.htm ]american sampler kidney table [/url] diet la loss review weight

Anonymous said...

euro millions cash grant promotion uk lottery board http://xwn.in/betting_free-betting-system real estate casino new south wales
[url=http://xwn.in/online-casino_pennsylvania-casino-exile-lebanon]sugar bowl betting line[/url] what diversity via lottery [url=http://xwn.in/joker_joker-gallery]joker gallery[/url]
game bonuses blackjack learn play http://xwn.in/betting_online-lotto-betting
[url=http://xwn.in/blackjack_busted-blackjack-racing]mega millions lottery number[/url] free no deposit casinos online [url=http://xwn.in/keno_keno-lottery-generator-wheel]keno lottery generator wheel[/url]
los vegas nonsmoking casinos http://xwn.in/online-casino_indian-casino-new-york bingo callers guide [url=http://xwn.in/poker-online_poker-table-and-chairs]poker table and chairs[/url]

Anonymous said...

cases australia interactive gambling act http://wqm.in/joker_always-a-joker-in-the-pack-lonely-clown blackjack 55 styrofoam
[url=http://wqm.in/joker_steve-miller-joker-lyrics-alternate]samsung blackjack free software[/url] old maid playing cards [url=http://wqm.in/lottery_all-wining-numbers-for-virginia-lottery-raffle]all wining numbers for virginia lottery raffle[/url]
esl worksheet bingo dauber http://wqm.in/slots_how-to-add-extra-pci-slots
[url=http://wqm.in/gambling-online_poker-winning-computer-gambling]batman joker artwork galleries[/url] missouri lottery check tickets [url=http://wqm.in/online-casino_casino-in-the-park]casino in the park[/url]
teen gambling competition http://wqm.in/slot_utility-pci-slot california cold case playing cards [url=http://wqm.in/blackjack_john-patracks-21st-century-blackjack-book]john patracks 21st century blackjack book[/url]

Anonymous said...

invasion movie poster [url=http://moviestrawberry.com/films/film_return_to_halloweentown/]return to halloweentown[/url] rush hour 3 the movie http://moviestrawberry.com/countries/?page=144 zeitgeist the movie
the rocks 2001 movie [url=http://moviestrawberry.com/films/film_the_steam_experiment/]the steam experiment[/url] discover mills mall movie theater http://moviestrawberry.com/films/film_closing_the_ring/ movie star baby pictures
movie downloader software [url=http://moviestrawberry.com/films/film_carrie/]carrie[/url] poseiden movie
movie atl [url=http://moviestrawberry.com/films/film_my_one_and_only/]my one and only[/url] movie maker mpg http://moviestrawberry.com/films/film_star_wars_episode_iii_revenge_of_the_sith/ star wars movie quotes audio
movie jaws affect on beach vacations [url=http://moviestrawberry.com/films/film_high_school_high/]high school high[/url] movie theaters raleigh http://moviestrawberry.com/films/film_exit_wounds/ robert deniro movie sounds

Anonymous said...

celebrity movie archive browse [url=http://moviestrawberry.com/films/film_i_love_your_work/]i love your work[/url] movie ost http://moviestrawberry.com/films/film_yj_toshi/ march 25 movie susan
deep throat movie clips [url=http://moviestrawberry.com/films/film_wildfire/]wildfire[/url] disney movie history http://moviestrawberry.com/films/film_the_jackal/ architectural style of house in housesitter movie
sexiest movie scenes [url=http://moviestrawberry.com/films/film_the_hours/]the hours[/url] coach carter movie clips download
porn movie thumb [url=http://moviestrawberry.com/films/film_black_woman_s_guide_to_finding_a_good_man/]black woman s guide to finding a good man[/url] jobs in the movie industry http://moviestrawberry.com/films/film_stranded/ download the pacifier movie free
hot hindi movie download [url=http://moviestrawberry.com/films/film_martian_child/]martian child[/url] tranformers the movie http://moviestrawberry.com/films/film_the_world_is_not_enough/ the brazilian job movie

Anonymous said...

making digital movie into still picture [url=http://moviestrawberry.com/films/film_mo_better_blues/]mo better blues[/url] movie brother sister stuffed animals magic door http://moviestrawberry.com/films/film_piccadilly_jim/ blah blah movie
movie date tips [url=http://moviestrawberry.com/films/film_austin_powers_in_goldmember/]austin powers in goldmember[/url] crack bittorrent movie http://moviestrawberry.com/countries/?page=38 elizabethtown movie
civil war movie [url=http://moviestrawberry.com/films/film_a_few_good_men/]a few good men[/url] movie industry in the philippines
fay wray classic movie film [url=http://moviestrawberry.com/films/film_red_riding_1980/]red riding 1980[/url] top movie reviews http://moviestrawberry.com/films/film_boogeyman_3/ farmingdale multiplex movie theater
free psp movie converter [url=http://moviestrawberry.com/films/film_the_genius_club/]the genius club[/url] movie screensavers http://moviestrawberry.com/films/film_stuntmen/ free cambodia sex movie

Anonymous said...

microsoft movie maker [url=http://moviestrawberry.com/films/film_hallettsville/]hallettsville[/url] shrek 2 movie credits http://moviestrawberry.com/films/film_infamy/ capote movie review
instruments used in the movie drumline [url=http://moviestrawberry.com/films/film_family_plot/]family plot[/url] killer wave movie http://moviestrawberry.com/hqmoviesbygenres/download-genre_fantasy-movies/?page=11 the ten deadly sins movie
movie 1408 at amazon [url=http://moviestrawberry.com/films/film_chitty_chitty_bang_bang/]chitty chitty bang bang[/url] hellraiser 2 movie downloads
boot leg movie down loads from canada [url=http://moviestrawberry.com/films/film_the_las_vegas_abductions/]the las vegas abductions[/url] et movie photographs http://moviestrawberry.com/films/film_social_lion/ lone wolf cub movie
movie devils brigade [url=http://moviestrawberry.com/films/film_teeth/]teeth[/url] prophet movie http://moviestrawberry.com/films/film_walled_in/ intel digital movie creator download