-
Notifications
You must be signed in to change notification settings - Fork 4
/
map.lisp
67 lines (58 loc) · 2.58 KB
/
map.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(in-package #:cleavir-bir)
(defmacro do-functions ((function module) &body body)
"Execute the BODY with FUNCTION bound to each function in MODULE. Arbitrary order."
`(set:doset (,function (functions ,module)) ,@body))
(defun map-functions (f module)
"Call F on each function in MODULE. Arbitrary order."
(do-functions (function module)
(funcall f function)))
(defmacro do-iblocks ((iblock function &optional (direction :forward)) &body body)
"Execute the BODY with IBLOCK bound to each iblock in FUNCTION.
DIRECTION may be :FORWARD for forward flow order, or :BACKWARD for reverse flow order."
(multiple-value-bind (from to)
(ecase direction
(:forward (values 'start '%next))
(:backward (values 'tail '%prev)))
`(do ((,iblock (,from ,function) (,to ,iblock)))
((null ,iblock))
,@body)))
(defun map-iblocks (f function)
"Call F on each iblock in FUNCTION, in forward flow order."
(do-iblocks (iblock function)
(funcall f iblock)))
(defmacro do-iblock-instructions ((instruction iblock
&optional (direction :forward))
&body body)
"Execute the BODY with INSTRUCTION bound to each instruction in IBLOCK.
DIRECTION may be :FORWARD or :BACKWARD."
(multiple-value-bind (from to)
(ecase direction
(:forward (values 'start 'successor))
(:backward (values 'end 'predecessor)))
`(do ((,instruction (,from ,iblock) (,to ,instruction)))
((null ,instruction) (values))
,@body)))
(defun map-iblock-instructions (f iblock)
"Call F on each instruction in IBLOCK, in forward order."
(check-type iblock iblock)
(do-iblock-instructions (instruction iblock)
(funcall f instruction)))
(defun map-iblock-instructions-backwards (f iblock)
"Call F on each instruction in IBLOCK, in backwards order."
(check-type iblock iblock)
(do-iblock-instructions (instruction iblock :backward)
(funcall f instruction)))
(defun map-local-instructions (f function)
"Call F on all instructions owned by FUNCTION, in forward flow order."
(do-iblocks (iblock function)
(map-iblock-instructions f iblock)))
(defun map-lambda-list (function lambda-list)
"This utility parses BIR lambda lists. FUNCTION takes three arguments: The state of the parse (e.g. &OPTIONAL), the current lambda-list item being parsed, and the index of the item."
(let ((state :required)
(index 0))
(dolist (item lambda-list)
(if (symbolp item)
(setq state item)
(progn
(funcall function state item index)
(incf index))))))