+ All Categories
Home > Documents > L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 ·...

L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 ·...

Date post: 10-Jul-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
9
9/14/17 1 CS 61C: Great Ideas in Computer Architecture More RISC-V Instructions and How to Implement Functions Instructors: Krste Asanović and Randy H. Katz http://inst.eecs.Berkeley.edu/~cs61c/fa17 9/14/17 Fall 2017 - Lecture #6 1 Outline RISC-V ISA and C-to-RISC-V Review Program Execution Overview Function Call Function Call Example And in Conclusion … 9/14/17 2 Outline RISC-V ISA and C-to-RISC-V Review Program Execution Overview Function Call Function Call Example And in Conclusion … 9/14/17 3 Levels of Representation/Interpretation lw x10, 0(x12) lw x11, 4(x12) sw x11, 0(x12) sw x10, 4(x12) High-Level Language Program (e.g., C) Assembly Language Program (e.g., RISC-V) Machine Language Program (RISC-V) Hardware Architecture Description (e.g., block diagrams) Compiler Assembler Machine Interpretation temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; 0000 1001 1100 0110 1010 1111 0101 1000 1010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 Architecture Implementation Anything can be represented as a number, i.e., data or instructions Logic Circuit Description (Circuit Schematic Diagrams) 9/14/17 4 Review From Last Lecture … Computer “words” and “vocabulary” are called instructions and instruction set respectively RISC-V is example RISC instruction set used in CS61C Lecture/problems use 32-bit RV32 ISA, book uses 64-bit RV64 ISA Rigid format: one operation, two source operands, one destination add,sub,mul,div,and,or,sll,srl,sra lw,sw,lb,sb to move data to/from registers from/to memory beq, bne, j for decision/flow control Simple mappings from arithmetic expressions, array access, in C to RISC-V instructions 9/14/17 5 Processor Control Datapath Recap: Registers live inside the Processor 6 PC Registers Arithmetic & Logic Unit (ALU) Memory Input Output Bytes Enable? Read/Write Address Write Data Read Data Processor-Memory Interface I/O-Memory Interfaces Program Data CS 61c
Transcript
Page 1: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

1

CS61C:GreatIdeasinComputerArchitecture

MoreRISC-VInstructionsandHowtoImplementFunctions

Instructors:KrsteAsanović andRandyH.Katz

http://inst.eecs.Berkeley.edu/~cs61c/fa17

9/14/17 Fall2017- Lecture#6 1

Outline• RISC-VISAandC-to-RISC-VReview• ProgramExecutionOverview• FunctionCall• FunctionCallExample• AndinConclusion…

9/14/17 2

Outline• RISC-VISAandC-to-RISC-VReview• ProgramExecutionOverview• FunctionCall• FunctionCallExample• AndinConclusion…

9/14/17 3

LevelsofRepresentation/Interpretation

lw x10,0(x12)lw x11,4(x12)sw x11,0(x12)sw x10,4(x12)

High-LevelLanguageProgram(e.g.,C)

AssemblyLanguageProgram(e.g.,RISC-V)

MachineLanguageProgram(RISC-V)

HardwareArchitectureDescription(e.g.,blockdiagrams)

Compiler

Assembler

MachineInterpretation

temp=v[k];v[k]=v[k+1];v[k+1]=temp;

0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111

ArchitectureImplementation

Anythingcanberepresentedasanumber,

i.e.,dataorinstructions

LogicCircuitDescription(CircuitSchematicDiagrams)

9/14/17 4

ReviewFromLastLecture…• Computer“words”and“vocabulary”arecalledinstructions and

instructionsetrespectively• RISC-VisexampleRISCinstructionsetusedinCS61C

– Lecture/problemsuse32-bitRV32ISA,bookuses64-bitRV64ISA• Rigidformat:oneoperation,twosourceoperands,onedestination

– add,sub,mul,div,and,or,sll,srl,sra– lw,sw,lb,sb tomovedatato/fromregistersfrom/tomemory– beq, bne, j fordecision/flowcontrol

• Simplemappingsfromarithmeticexpressions,arrayaccess,inCtoRISC-Vinstructions

9/14/17 5

Processor

Control

Datapath

Recap:RegistersliveinsidetheProcessor

6

PC

RegistersArithmetic&LogicUnit

(ALU)

Memory Input

Output

Bytes

Enable?Read/Write

Address

WriteData

ReadData

Processor-MemoryInterface I/O-MemoryInterfaces

Program

Data

CS61c

Page 2: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

2

Exampleif-else Statement• Assumingtranslationsbelow,compilef→x10 g→x11 h→x12i →x13 j→x14

if (i == j) bne x13,x14,Else f = g + h; add x10,x11,x12

else j Exit f = g – h; Else: sub x10,x11,x12

Exit:

9/14/17 7

Magnitude Compares in RISC-V• Untilnow,we’veonlytestedequalities(==and!=inC);

Generalprogramsneedtotest<and>aswell.• RISC-Vmagnitude-comparebranches:

“BranchonLessThan”Syntax:blt reg1,reg2, labelMeaning: if(reg1<reg2)//treatregistersassignedintegers

goto label;• “BranchonLessThanUnsigned”

Syntax:bltu reg1,reg2, label

Meaning: if(reg1<reg2) //treatregistersasunsignedintegersgoto label;

9/14/17 8

CLoopMappedtoRISC-VAssemblyint A[20];int sum = 0;for (int i=0; i<20; i++)

sum += A[i];

addi x9, x8, 0 # x9=&A[0]addi x10, x0, 0 # sum=0addi x11, x0, 0 # i=0

Loop:lw x12, 0(x9) # x12=A[i]add x10,x10,x12 # sum+=addi x9,x9,4 # &A[i++]addi x11,x11,1 # i++addi x13,x0,20 # x13=20blt x11,x13,Loop

9

PeerInstructionWhichofthefollowingisTRUE?

RED:add x10,x11,4(x12)isvalidinRV32GREEN:canbyteaddress8GBofmemorywithanRV32wordORANGE:immmustbemultipleof4forlw x10,imm(x10)tobevalid

:Noneoftheabove

9/14/17 10

PeerInstructionWhichofthefollowingisTRUE?

RED:add x10,x11,4(x12)isvalidinRV32GREEN:canbyteaddress8GBofmemorywithanRV32wordORANGE:immmustbemultipleof4forlw x10,imm(x10)tobevalid

:Noneoftheabove

9/14/17 11

Outline• RISC-VISAandC-to-RISC-VReview• ProgramExecutionOverview• FunctionCall• FunctionCallExample• AndinConclusion…

9/14/17 12

Page 3: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

3

AssemblertoMachineCode(morelaterincourse)

foo.S bar.S

Assembler Assembler

foo.o bar.o

Linker lib.o

a.out

Assemblersourcefiles(text)

Machinecodeobjectfiles

Pre-builtobjectfilelibraries

Machinecodeexecutablefile

Assemblerconvertshuman-readableassemblycodetoinstructionbitpatterns

9/14/17 13

HowProgramisStoredMemory

Bytes

Program

Data

OneRISC-VInstruction=32bits

9/14/17 14

Processor

Control

Datapath

ProgramExecution

PC

RegistersArithmetic&LogicUnit

(ALU)

Memory

BytesInstructionAddress

ReadInstructionBits

Program

Data

• PC (programcounter)isinternalregisterinsideprocessorholdingbyte addressofnextinstructiontobeexecuted

• Instructionisfetchedfrommemory,thencontrolunitexecutesinstructionusingdatapath andmemorysystem,andupdatesprogramcounter(defaultisadd+4bytestoPC,tomovetonextsequentialinstruction)

9/14/17 15

IntheNews:Whyfastcomputersmatter

CS61c 16

EuropeanWeathersupercomputerECMWF50tonnes~120,000computecores(IntelBroadwell)10PetaBytes ofstorageRunsLinuxoneachnode

Break!

9/14/17 17

HelpfulRISC-VAssemblerFeatures

• Symbolicregisternames– E.g.,a0-a7 forargumentregisters(x10-x17)– E.g.,zero forx0

• Pseudo-instructions– Shorthandsyntaxforcommonassemblyidioms– E.g.,mv rd, rs = addi rd, rs, 0– E.g.2,li rd, 13 = addi rd, x0, 13

18

Page 4: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

4

RISC-VSymbolicRegisterNames

19

Numbershardwareunderstands

Human-friendlysymbolicnamesinassemblycode

Outline• RISC-VISAandC-to-RISC-VReview• ProgramExecutionOverview• FunctionCall• FunctionCallExample• AndinConclusion…

9/14/17 20

SixFundamentalStepsinCallingaFunction

1. Putparametersinaplacewherefunctioncanaccessthem

2. Transfercontroltofunction3. Acquire(local)storageresourcesneededforfunction4. Performdesiredtaskofthefunction5. Putresultvalueinaplacewherecallingcodecan

accessitandrestoreanyregistersyouused6. Returncontroltopointoforigin,sinceafunctioncan

becalledfromseveralpointsinaprogram9/14/17 21

RISC-VFunctionCallConventions• Registersfasterthanmemory,sousethem• a0–a7 (x10-x17):eightargumentregisterstopassparametersandtworeturnvalues(a0-a1)

• ra:onereturnaddressregistertoreturntothepointoforigin(x1)

9/14/17 22

InstructionSupportforFunctions(1/4)... sum(a,b);... /* a,b:s0,s1 */}int sum(int x, int y) {return x+y;}

address (shown in decimal)1000 1004 1008 1012 1016 …2000 2004

CRISC-V

InRISC-V,allinstructionsare4bytes,andstoredinmemoryjustlikedata.Sohereweshowtheaddressesofwheretheprogramsarestored.

9/14/17 23

InstructionSupportforFunctions(2/4)... sum(a,b);... /* a,b:s0,s1 */}int sum(int x, int y) {return x+y;}

address (shown in decimal)1000 mv a0,s0 # x = a1004 mv a1,s1 # y = b1008 addi ra,zero,1016 #ra=10161012 j sum #jump to sum1016 … # next instruction…2000 sum: add a0,a0,a12004 jr ra # new instr. “jump register”

9/14/17 24

CRISC-V

Page 5: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

5

InstructionSupportforFunctions(3/4)... sum(a,b);... /* a,b:s0,s1 */}int sum(int x, int y) {return x+y;}

2000 sum: add a0,a0,a12004 jr ra # new instr. “jump register”

• Question:Whyuse jr here?Whynot usej?

• Answer:summightbecalledbymanyplaces,sowecan’treturntoafixedplace.Thecallingproctosummustbeabletosay“returnhere”somehow.

9/14/17 25

CRISC-V

InstructionSupportforFunctions(4/4)• Singleinstructiontojumpandsavereturnaddress:jumpandlink

(jal)• Before:

1008 addi ra,zero,1016 #ra=10161012 j sum #goto sum

• After:1008 jal sum # ra=1012,goto sum

• Whyhaveajal?– Makethecommoncasefast:functioncalls verycommon– Reduceprogramsize– Don’thavetoknowwhere codeis inmemorywithjal!

9/14/17 26

RISC-VFunctionCallInstructions• Invokefunction:jumpandlinkinstruction(jal)

(reallyshouldbelaj “linkandjump”)– “link”meansformanaddressorlinkthatpointsto

callingsitetoallowfunctiontoreturntoproperaddress– Jumpstoaddressandsimultaneouslysavestheaddressofthefollowing

instructioninregisterrajal FunctionLabel

• Returnfromfunction:jumpregisterinstruction(jr)– Unconditionaljumptoaddressspecifiedinregister:jr ra– Assemblershorthand:ret = jr ra

9/14/17 27

Outline• RISC-VISAandC-to-RISC-VReview• ProgramExecutionOverview• FunctionCall• FunctionCallExample• AndinConclusion…

9/14/17 28

Exampleint Leaf

(int g, int h, int i, int j){

int f;f = (g + h) – (i + j);return f;

}• Parametervariablesg,h,i, andj inargumentregistersa0,a1,

a2,anda3,andf ins0• Assumeneedonetemporaryregisters1

9/14/17 29

WhereAreOldRegisterValuesSavedtoRestoreThemAfterFunctionCall?• Needaplacetosaveoldvaluesbeforecallfunction,restore

themwhenreturn,anddelete• Idealisstack:last-in-first-outqueue

(e.g.,stackofplates)– Push:placingdataontostack– Pop:removingdatafromstack

• Stackinmemory,soneedregistertopointtoit• sp isthestackpointerinRISC-V(x2)• Conventionisgrowstackdownfromhightolowaddresses

– Push decrementssp,Pop incrementssp9/14/17 30

Page 6: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

6

RISC-VCodeforLeaf()Leaf: addi sp,sp,-8 # adjust stack for 2 items

sw s1, 4(sp) # save s1 for use afterwardssw s0, 0(sp) # save s0 for use afterwards

add s0,a0,a1 # f = g + hadd s1,a2,a3 # s1 = i + jsub a0,s0,s1 # return value (g + h) – (i + j)

lw s0, 0(sp) # restore register s0 for caller lw s1, 4(sp) # restore register s1 for calleraddi sp,sp,8 # adjust stack to delete 2 itemsjr ra # jump back to calling routine

9/14/17 31

StackBefore,During,AfterFunction• Needtosaveoldvaluesofs0 ands1

9/14/17 32

sp

Beforecall

spSaved s1

Duringcall

Saved s0

sp

Aftercall

Saved s1Saved s0

Administrivia• HW1isout!Getstartedearly.• CandMemoryManagementGuerrillaSessionistonight7-9pmin293Cory

• Smallgrouptutoringsessionshavelaunched

9/14/17 33

NewRISC-Vbook!• “TheRISC-VReader”,DavidPatterson,

AndrewWaterman

• Availableat• https://www.createspace.com/7439283

• Earlyprintedition$9.99• Kindleeditiontofollowatsomepoint

• Recommended,notrequired

34

Break!

9/14/17 35

WhatIfaFunctionCallsaFunction?RecursiveFunctionCalls?

• Wouldclobbervaluesina0-a7 andra• Whatisthesolution?

9/14/17 36

Page 7: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

7

NestedProcedures(1/2)int sumSquare(int x, int y) {return mult(x,x)+ y;}

• SomethingcalledsumSquare,nowsumSquare iscallingmult

• Sothere’savalueinra thatsumSquarewantstojumpbackto,butthiswillbeoverwrittenbythecalltomult

NeedtosavesumSquare returnaddressbeforecalltomult

9/14/17 37

NestedProcedures(2/2)• Ingeneral,mayneedtosavesomeotherinfoinadditiontora.

• WhenaCprogramisrun,therearethreeimportantmemoryareasallocated:– Static:Variablesdeclaredonceperprogram,ceasetoexistonlyafterexecutioncompletes- e.g.,Cglobals

– Heap:Variablesdeclareddynamicallyviamalloc– Stack:Spacetobeusedbyprocedureduringexecution;thisiswherewecansaveregistervalues

389/14/17

OptimizedFunctionConventionToreduceexpensiveloadsandstoresfromspillingandrestoringregisters,RISC-Vfunction-callingconventiondividesregistersintotwocategories:

1. Preservedacrossfunctioncall– Callercanrelyonvaluesbeingunchanged– sp,gp,tp, “savedregisters”s0- s11 (s0 isalso fp)

2. Notpreservedacrossfunctioncall– Callercannotrelyonvaluesbeingunchanged– Argument/returnregistersa0-a7,ra,“temporary

registers”t0-t69/14/17 39

PeerInstruction• WhichstatementisFALSE?• RED:RISC-Vusesjal toinvokeafunctionandjr toreturnfromafunction

• GREEN: jal savesPC+1inra• ORANGE: Thecallee canusetemporaryregisters(ti)

withoutsavingandrestoringthem: Thecallercanrelyonsaveregisters(si)

withoutfearofcallee changingthem

9/14/17 40

PeerInstruction• WhichstatementisFALSE?• RED:RISC-Vusesjal toinvokeafunctionandjr toreturnfromafunction

• GREEN: jal savesPC+1inra• ORANGE: Thecallee canusetemporaryregisters(ti)

withoutsavingandrestoringthem: Thecallercanrelyonsaveregisters(si)

withoutfearofcallee changingthem

9/14/17 41

AllocatingSpaceonStack• Chastwostorageclasses:automaticandstatic– Automatic variablesarelocaltofunctionanddiscardedwhenfunctionexits

– Staticvariablesexistacrossexitsfromandentriestoprocedures

• Usestackforautomatic(local)variablesthatdon’tfitinregisters

• Procedureframeor activationrecord:segmentofstackwithsavedregistersandlocalvariables

9/14/17 42

Page 8: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

8

StackBefore,During,AfterFunction

439/14/17

sp

Beforecall

sp

Duringcall

Savedargumentregisters(ifany)

Savedreturnaddress(ifneeded)

Savedsavedregisters(ifany)Localvariables

(ifany)

sp

Aftercall

UsingtheStack(1/2)• Sowehavearegistersp whichalwayspointstothelastusedspaceinthestack

• Tousestack,wedecrementthispointerbytheamountofspaceweneedandthenfillitwithinfo

• So,howdowecompilethis?int sumSquare(int x, int y) {

return mult(x,x)+ y;}

9/14/17 44

UsingtheStack(2/2)

45

sumSquare: addi sp,sp,-8 # space on stacksw ra, 4(sp) # save ret addrsw a1, 0(sp) # save ymv a1,a0 # mult(x,x)jal mult # call multlw a1, 0(sp) # restore yadd a0,a0,a1 # mult()+ylw ra, 4(sp) # get ret addraddi sp,sp,8 # restore stackjr ra

mult: ...

int sumSquare(int x, int y) {return mult(x,x)+ y; }

“push”

“pop”

9/14/17

WhereistheStackinMemory?• RV32convention(RV64andRV128havedifferentmemorylayouts)• Stackstartsinhighmemoryandgrowsdown

– Hexadecimal(base16):bfff_fff0hex– Stackmustbealignedon16-byteboundary(nottrueinexamplesabove)

• RV32programs(textsegment)inlowend– 0001_0000hex

• staticdatasegment(constantsandotherstaticvariables)abovetextforstaticvariables– RISC-Vconventionglobalpointer(gp)pointstostatic– RV32gp =1000_0000hex

• Heapabovestaticfordatastructuresthatgrowandshrink;growsuptohighaddresses

9/14/17 46

RV32MemoryAllocation

9/14/17 47

Outline• RISC-VISAandC-to-RISC-VReview• ProgramExecutionOverview• FunctionCall• FunctionCallExample• AndinConclusion…

9/14/17 48

Page 9: L06 RISCV Functions - University of California, Berkeleycs61c/fa17/lec/06/L06... · 2017-09-15 · 9/14/17 3 Assembler to Machine Code (more later in course) foo.S bar.S Assembler

9/14/17

9

AndinConclusion…• Functionscalledwithjal,returnwithjr ra.• Thestackisyourfriend:Useittosaveanythingyouneed.Just leaveitthewayyou

foundit!• Instructionsweknowsofar…

Arithmetic:add, addi, subMemory: lw, sw, lb, lbu, sbDecision:beq, bne, blt, bgeUnconditionalBranches(Jumps):j, jal, jr

• Registersweknowsofar– Allofthem!– a0-a7forfunctionarguments,a0-a1forreturnvalues– sp,stackpointer, ra returnaddress– s0-s11savedregisters– t0-t6temporaries– zero

499/14/17


Recommended