A CIL Tutorial - Department of Computer Science - ETH Zürich
A CIL Tutorial - Department of Computer Science - ETH Zürich A CIL Tutorial - Department of Computer Science - ETH Zürich
Chapter 7 Type Qualiers Over the next three chapters, we'll explore how to make changes to C's type-system. This will be achieved by adding type-qualiers to C's types, and by performing some extra type-checking. In this section, we'll write a very basic type-checker for types that may be qualied by one or more of the following colors: red, green, or blue. In the exercises, you'll nd suggestions about how to complete it. In the next section, we'll look at interpreting dependent type qualiers. Finally, in Chapter 9, we'll see how to do some basic type qualier inference. 7.1 tut7.ml In tut7.ml, rst we'll write functions to extract qualiers from types. Then, we'll perform our additional type-checking. 7.1.1 Qualier Types We'll dene some OCaml types representing the C type qualiers. Then, from C types, we'll extract a possibly empty list of the qualiers. type color = Red | Blue | Green We'll set up some global constants for the string representation of the qualiers, and use them everywhere instead of the strings, in case we want to change them later on. let redStr = "red" let blueStr = "blue" let greenStr = "green" Putting the strings in a list will help a bit later on. 52
CHAPTER 7. TYPE QUALIFIERS 53 let color strings = [redStr; blueStr; greenStr; ] As mentioned in a previous chapter, it is useful to have functions that convert a type to and from a string. The function string of color returns redStr, blueStr, or greenStr as appropriate. let string of color (c : color) : string = match c with | Red → redStr | Blue → blueStr | Green → greenStr The function color of string returns the color corresponding to the string cs it gets as input. let color of string (cs : string) : color = match S.lowercase cs with | s when s = redStr → Red | s when s = blueStr → Blue | s when s = greenStr → Green | → E.s(E.bug "Expected a color string, got: %s" cs) The function isColorType returns true when a type is qualied by a particular color attribute. The function isTypeColor the same with the order of the arguments reversed. The next three functions tell if a type is qualied by a particular color. These functions to largely similar things, but they'll be useful in dierent situations. isColorType is written with the function hasAttribute, which comes from the core Cil module. It returns true when a list of attributes contains an attribute with the given name. typeAttrs extracts the attributes from a type. Attributes are one of the extensions to C accepted by gcc. CIL parses these attributes, even custom attributes not understood by gcc, and includes them in its AST attached to types, elds, functions, formal parameters, and blocks of code. All we care about for now, though, is whether or not a type is qualied by one of the colors. let isColorType (cs : string) (t : typ) : bool = hasAttribute cs (typeAttrs t) let isTypeColor (t : typ) (cs : string) : bool = isColorType cs t let isRedType : typ → bool = isColorType redStr let isBlueType : typ → bool = isColorType blueStr let isGreenType : typ → bool = isColorType greenStr The function colors of type takes a type and returns a list of colors that qualify the type.
- Page 3 and 4: Contents Preface 4 Introduction 5 0
- Page 5 and 6: CONTENTS 3 13 Whole-program Analysi
- Page 7 and 8: Introduction The C Intermediate Lan
- Page 9 and 10: References [1] clang: a C language
- Page 11 and 12: CHAPTER 0. OVERVIEW AND ORGANIZATIO
- Page 13 and 14: Chapter 1 The AST The Concrete Synt
- Page 15 and 16: CHAPTER 1. THE AST 13 1.2 Printing
- Page 17 and 18: References [1] Andrew W. Appel. Mod
- Page 19 and 20: CHAPTER 2. VISITING THE AST 17 open
- Page 21 and 22: CHAPTER 2. VISITING THE AST 19 $ ci
- Page 23 and 24: Chapter 3 Dataow Analysis Dataow An
- Page 25 and 26: CHAPTER 3. DATAFLOW ANALYSIS 23 Cod
- Page 27 and 28: CHAPTER 3. DATAFLOW ANALYSIS 25 let
- Page 29 and 30: CHAPTER 3. DATAFLOW ANALYSIS 27 and
- Page 31 and 32: CHAPTER 3. DATAFLOW ANALYSIS 29 let
- Page 33 and 34: CHAPTER 3. DATAFLOW ANALYSIS 31 DoC
- Page 35 and 36: CHAPTER 3. DATAFLOW ANALYSIS 33 tes
- Page 37 and 38: References [1] Aws Albarghouthi, Ra
- Page 39 and 40: CHAPTER 4. INSTRUMENTATION 37 type
- Page 41 and 42: CHAPTER 4. INSTRUMENTATION 39 metho
- Page 43 and 44: CHAPTER 4. INSTRUMENTATION 41 $ cil
- Page 45 and 46: CHAPTER 5. INTERPRETED CONSTRUCTORS
- Page 47 and 48: CHAPTER 5. INTERPRETED CONSTRUCTORS
- Page 49 and 50: Chapter 6 Overriding Functions When
- Page 51 and 52: CHAPTER 6. OVERRIDING FUNCTIONS 49
- Page 53: References [1] Kumar Avijit, Pratee
- Page 57 and 58: CHAPTER 7. TYPE QUALIFIERS 55 let w
- Page 59 and 60: CHAPTER 7. TYPE QUALIFIERS 57 $ cil
- Page 61 and 62: Chapter 8 Dependant Type Qualiers O
- Page 63 and 64: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 65 and 66: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 67 and 68: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 69 and 70: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 71 and 72: CHAPTER 8. DEPENDANT TYPE QUALIFIER
- Page 73 and 74: Chapter 9 Type Qualier Inference In
- Page 75 and 76: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 77 and 78: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 79 and 80: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 81 and 82: CHAPTER 9. TYPE QUALIFIER INFERENCE
- Page 83 and 84: Chapter 10 Adding a New Kind of Sta
- Page 85 and 86: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 87 and 88: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 89 and 90: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 91 and 92: CHAPTER 10. ADDING A NEW KIND OF ST
- Page 93 and 94: Chapter 11 Program Verication In th
- Page 95 and 96: CHAPTER 11. PROGRAM VERIFICATION 93
- Page 97 and 98: CHAPTER 11. PROGRAM VERIFICATION 95
- Page 99 and 100: CHAPTER 11. PROGRAM VERIFICATION 97
- Page 101 and 102: CHAPTER 11. PROGRAM VERIFICATION 99
- Page 103 and 104: CHAPTER 11. PROGRAM VERIFICATION 10
Chapter 7<br />
Type Qualiers<br />
Over the next three chapters, we'll explore how to make changes to C's type-system. This will be<br />
achieved by adding type-qualiers to C's types, and by performing some extra type-checking. In<br />
this section, we'll write a very basic type-checker for types that may be qualied by one or more<br />
<strong>of</strong> the following colors: red, green, or blue. In the exercises, you'll nd suggestions about how to<br />
complete it. In the next section, we'll look at interpreting dependent type qualiers. Finally, in<br />
Chapter 9, we'll see how to do some basic type qualier inference.<br />
7.1 tut7.ml<br />
In tut7.ml, rst we'll write functions to extract qualiers from types. Then, we'll perform our<br />
additional type-checking.<br />
7.1.1 Qualier Types<br />
We'll dene some OCaml types representing the C type qualiers. Then, from C types, we'll extract<br />
a possibly empty list <strong>of</strong> the qualiers.<br />
type color = Red | Blue | Green<br />
We'll set up some global constants for the string representation <strong>of</strong> the qualiers, and use them<br />
everywhere instead <strong>of</strong> the strings, in case we want to change them later on.<br />
let redStr = "red"<br />
let blueStr = "blue"<br />
let greenStr = "green"<br />
Putting the strings in a list will help a bit later on.<br />
52