aboutsummaryrefslogtreecommitdiff
path: root/Makerules
blob: 21b260cc9aa17569e078dc7879f8de5d21ff5d44 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
# This file is part of the GNU C Library.

# The GNU C Library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.

# The GNU C Library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.

# You should have received a copy of the GNU Library General Public
# License along with the GNU C Library; see the file COPYING.LIB.  If
# not, write to the Free Software Foundation, Inc., 675 Mass Ave,
# Cambridge, MA 02139, USA.

#
#	Common rules for making the GNU C library.  This file is included
#	by the top-level Makefile and by all subdirectory makefiles
#	(through Rules).
#
ifneq (,)
This makefile requires GNU Make.
endif


ifdef	subdir
..	:= ../
endif	# subdir

# If `sources' was defined by the parent makefile, undefine it so
# we will later get it from wildcard search in this directory.
ifneq	"$(findstring env,$(origin sources))" ""
sources :=
endif

headers := $(headers) $(sysdep_headers)

oPATH := $(PATH)
PATH := this definition should take precedence over $(oPATH)
ifeq ($(PATH),$(oPATH))
You must not use the -e flag when building the GNU C library.
else
PATH := $(oPATH)
endif

ifndef +included-Makeconfig
include $(..)Makeconfig
endif

# `configure' writes a definition of `config-sysdirs' in `config.make'.
sysdirs = $(config-sysdirs)

+sysdir_pfx = $(common-objpfx)

export sysdirs := $(sysdirs)

+sysdep_dirs := $(addprefix $(sysdep_dir)/,$(sysdirs))
ifdef objdir
+sysdep_dirs := $(objdir) $(+sysdep_dirs)
endif

# Add -I switches to get the right sysdep directories.
# `+includes' in Makeconfig references $(+sysdep-includes).
+sysdep-includes := $(addprefix -I,$(+sysdep_dirs))

# The name of the symbol table archive member.  The default is suitable for
# BSD style archives.  It can be overridden in sysdep Makefiles when SYSV
# style archive are used.
ifeq (no,$(elf))
ar-symtab-name = __.SYMDEF
else
ar-symtab-name = # The null name is used in ELF archives.
endif

# Include any system-specific makefiles.

# This is here so things in sysdep Makefiles can easily depend on foo.h as
# appropriate and not worry about where foo.h comes from, which may be
# system dependent and not known by that Makefile.
vpath %.h $(subst $(empty) ,:,$(strip $(common-objpfx) \
				      $(addprefix $(sysdep_dir)/,$(sysdirs)) \
				      $(..)))

# Some sysdep makefiles use this to distinguish being included here from
# being included individually by a subdir makefile (hurd/Makefile needs this).
in-Makerules := yes

ifndef avoid-generated
-include $(+sysdir_pfx)sysd-Makefile
ifneq ($(sysd-Makefile-sysdirs),$(sysdirs))
sysd-Makefile-force = FORCE
FORCE:
endif
$(+sysdir_pfx)sysd-Makefile: $(+sysdir_pfx)config.make $(..)Makerules \
			     $(sysd-Makefile-force)
	-@rm -f $@T
	(echo 'sysd-Makefile-sysdirs := $(sysdirs)';			      \
	 for dir in $(sysdirs); do		   			      \
	   file=sysdeps/$$dir/Makefile;					      \
	   if [ -f $(..)$$file ]; then					      \
	     echo include "\$$(..)$$file";				      \
	   else true; fi;						      \
	 done; \
	 echo 'sysd-Makefile-done=t') > $@T
	mv -f $@T $@
endif

ifndef sysd-Makefile-done
# Don't do deps until this exists, because it might change the sources list.
no_deps=t
endif


# Reorder before-compile so that mach things come first, and hurd things
# second, before all else.  The mach and hurd subdirectories have many
# generated header files which the much of rest of the library depends on,
# so it is best to build them first (and mach before hurd, at that).
before-compile := $(filter $(common-objpfx)mach% $(common-objpfx)hurd%,\
			   $(before-compile)) \
		  $(filter-out $(common-objpfx)mach% $(common-objpfx)hurd%,\
			       $(before-compile))

# Remove existing files from `before-compile'.  Things are added there when
# they must exist for dependency generation to work right, but once they
# exist there is no further need for every single file to depend on them,
# and those gratuitous dependencies result in many gratuitous
# recompilations.
before-compile := $(filter-out $(wildcard $(before-compile)),$(before-compile))

# Don't let any before-compile file be an intermediate and get removed.
ifdef before-compile
$(before-compile):
endif

# Generate an ordered list of implicit rules which find the source files in
# each sysdep directory.  The old method was to use vpath to search all the
# sysdep directories.  However, that had the problem that a .S file in a
# later directory would be chosen over a .c file in an earlier directory,
# which does not preserve the desired sysdeps ordering behavior.

# When making the list of .d files to include, we can't know which ones
# have source in .s files, and thus do not in fact need a .d file.
# So we must define rules to make .d files for .s files.
define make-dummy-dep
$(addprefix ln $(objpfx)dummy.d ,$(filter-out $(wildcard $@),$@))
endef
$(objpfx)dummy.d:
	echo '# .s files cannot contain includes, so they need no deps.' > $@

# It matters that this set of rules, for compiling from sources in
# the current directory (the $srcdir/$subdir) come before the
# generated sysdep rules in included from sysd-rules below.  When
# compiling in the source tree, generated sources go into the current
# directory, and those should be chosen before any sources in sysdeps.
$(objpfx)%.o: %.S $(before-compile); $(compile-command.S)
$(objpfx)%.so: %.S $(before-compile); $(compile-command.S)
$(objpfx)%.po: %.S $(before-compile); $(compile-command.S)
$(objpfx)%.go: %.S $(before-compile); $(compile-command.S)
$(objpfx)%.d: %.S $(before-compile); $(+make-deps)
$(objpfx)%.o: %.s $(before-compile); $(compile-command.s)
$(objpfx)%.so: %.s $(before-compile); $(compile-command.s)
$(objpfx)%.po: %.s $(before-compile); $(compile-command.s)
$(objpfx)%.go: %.s $(before-compile); $(compile-command.s)
$(objpfx)%.d: %.s $(objpfx)dummy.d; $(make-dummy-dep)
$(objpfx)%.o: %.c $(before-compile); $(compile-command.c)
$(objpfx)%.so: %.c $(before-compile); $(compile-command.c)
$(objpfx)%.po: %.c $(before-compile); $(compile-command.c)
$(objpfx)%.go: %.c $(before-compile); $(compile-command.c)
$(objpfx)%.d: %.c $(before-compile); $(+make-deps)

# Omit the objpfx rules when building in the source tree, because
# objpfx is empty and so these rules just override the ones above.
ifdef objpfx
# Define first rules to find the source files in $(objpfx).
# Generated source files will end up there.
$(objpfx)%.o: $(objpfx)%.S $(before-compile); $(compile-command.S)
$(objpfx)%.so: $(objpfx)%.S $(before-compile); $(compile-command.S)
$(objpfx)%.po: $(objpfx)%.S $(before-compile); $(compile-command.S)
$(objpfx)%.go: $(objpfx)%.S $(before-compile); $(compile-command.S)
$(objpfx)%.d: $(objpfx)%.S $(before-compile); $(+make-deps)
$(objpfx)%.o: $(objpfx)%.s $(before-compile); $(compile-command.s)
$(objpfx)%.so: $(objpfx)%.s $(before-compile); $(compile-command.s)
$(objpfx)%.po: $(objpfx)%.s $(before-compile); $(compile-command.s)
$(objpfx)%.go: $(objpfx)%.s $(before-compile); $(compile-command.s)
$(objpfx)%.d: $(objpfx)%.s $(objpfx)dummy.d; $(make-dummy-dep)
$(objpfx)%.o: $(objpfx)%.c $(before-compile); $(compile-command.c)
$(objpfx)%.so: $(objpfx)%.c $(before-compile); $(compile-command.c)
$(objpfx)%.po: $(objpfx)%.c $(before-compile); $(compile-command.c)
$(objpfx)%.go: $(objpfx)%.c $(before-compile); $(compile-command.c)
$(objpfx)%.d: $(objpfx)%.c $(before-compile); $(+make-deps)
endif

# System-dependent makefiles can put in `inhibit-sysdep-asm' wildcard
# patterns matching sysdep directories whose assembly source files should
# be suppressed.
ifdef inhibit-sysdep-asm
define open-check-inhibit-asm
case $$sysdir in $(subst $(empty) ,|,$(inhibit-sysdep-asm))) : ;; *)
endef
close-check-inhibit-asm = ;; esac ;
endif

# Don't include sysd-rules until sysd-Makefile is already there and has been
# included.  It might define inhibit-sysdep-asm, which would affect the
# contents of sysd-rules.
ifdef sysd-Makefile-done
-include $(+sysdir_pfx)sysd-rules
ifneq ($(sysd-rules-sysdirs),$(sysdirs))
# The value of $(sysdirs) the sysd-rules was computed for
# differs from the one we are using now.  So force a rebuild of sysd-rules.
sysd-rules-force = FORCE
FORCE:
endif
endif
$(+sysdir_pfx)sysd-rules: $(+sysdir_pfx)config.make $(..)Makerules \
			  $(wildcard $(foreach dir,$(sysdirs),\
					       $(sysdep_dir)/$(dir)/Makefile))\
			  $(sysd-rules-force)
	-@rm -f $@T
	(echo 'sysd-rules-sysdirs := $(sysdirs)';			      \
	 for sysdir in $(sysdirs); do		   			      \
	   dir="\$$(sysdep_dir)/$$sysdir";				      \
	   for o in $(object-suffixes); do \
	     $(open-check-inhibit-asm) \
	     echo "\$$(objpfx)%$$o: $$dir/%.S \$$(before-compile); \
		  \$$(compile-command.S)";				      \
	     echo "\$$(objpfx)%$$o: $$dir/%.s \$$(before-compile); \
		  \$$(compile-command.s)";				      \
	     $(close-check-inhibit-asm)	\
	     echo "\$$(objpfx)%$$o: $$dir/%.c \$$(before-compile); \
		  \$$(compile-command.c)";				      \
	   done; \
	   $(open-check-inhibit-asm) \
	   echo "\$$(objpfx)%.d: $$dir/%.s \$$(objpfx)dummy.d; \
		\$$(make-dummy-dep)";			       \
	   echo "\$$(objpfx)%.d: $$dir/%.S \$$(before-compile); \
		\$$(+make-deps)";					      \
	   $(close-check-inhibit-asm)	\
	   echo "\$$(objpfx)%.d: $$dir/%.c \$$(before-compile); \
		\$$(+make-deps)";					      \
	 done;								      \
	 echo 'sysd-rules-done = t') > $@T
	mv -f $@T $@

ifndef sysd-rules-done
# Don't do deps until this exists, because it provides rules to make the deps.
no_deps=t
endif


ifndef compile-command.S
compile-command.S = $(compile.S) $(OUTPUT_OPTION)
endif
ifndef compile-command.s
compile-command.s = $(COMPILE.s) $< $(OUTPUT_OPTION)
endif
ifndef compile-command.c
compile-command.c = $(compile.c) $(OUTPUT_OPTION)
endif

# GCC can grok options after the file name, and it looks nicer that way.
compile.c = $(CC) $< -c $(CFLAGS) $(CPPFLAGS)
compile.S = $(CC) $< -c $(CPPFLAGS) -DASSEMBLER $(asm-CPPFLAGS)
COMPILE.S = $(CC) -c $(CPPFLAGS) -DASSEMBLER $(asm-CPPFLAGS)
COMPILE.s = $(filter-out -pipe,$(CC)) -c $(ASFLAGS)

# We need this for the output to go in the right place.  It will default to
# empty if make was configured to work with a cc that can't grok -c and -o
# together.  You can't compile the C library with such a compiler.
OUTPUT_OPTION = -o $@

S-CPPFLAGS = $(asm-CPPFLAGS)
define +make-deps
-@rm -f $@
$(+mkdep) $< $(CPPFLAGS) $($(<:$*.%=%)-CPPFLAGS) | \
sed -e 's,$*\.o,$(foreach o,$(object-suffixes),$(@:.d=$o)) $@,' \
$(sed-remove-objpfx) > $(@:.d=.T)
mv -f $(@:.d=.T) $@
endef
ifneq (,$(objpfx))
sed-remove-objpfx = -e 's@ $(subst @,\@,$(objpfx))@ $$(objpfx)@g' \
		    -e 's@^$(subst @,\@,$(objpfx))@$$(objpfx)@g'
endif

# Figure out the source filenames in this directory.

override sources	:= $(addsuffix .c,$(filter-out $(elided-routines),\
						       $(routines) $(aux) \
						       $(sysdep_routines)))
sysdep_routines := $(sysdep_routines)

# This is the list of all object files, gotten by
# replacing every ".c" in `sources' with a ".o".
override objects	:= $(addprefix $(objpfx),$(sources:.c=.o))


# This variable is used in ``include $(o-iterator)'' after defining
# $(o-iterator-doit) to produce some desired rule using $o for the object
# suffix, and setting $(object-suffixes-left) to $(object-suffixes); a copy
# is produced for each object suffix in use.
o-iterator = $(patsubst %,$(..)o-iterator.mk,$(object-suffixes))

# The makefile may define $(extra-libs) with `libfoo libbar'
# to build libfoo.a et al from the modules listed in $(libfoo-routines).
ifdef extra-libs
# extra-lib.mk is included once for each extra lib to define rules
# to build it, and to add its objects to the various variables.
# During its evaluation, $(lib) is set to the name of the library.
extra-libs-left := $(extra-libs)
include $(patsubst %,$(..)extra-lib.mk,$(extra-libs))
endif

+depfiles := $(strip $(sources:.c=.d) \
		     $(patsubst %.o,%.d,$(filter %.o,$(extra-objs:.so=.o))) \
		     $(addsuffix .d,$(others) $(tests)))
+depfiles := $(addprefix $(objpfx),\
			 $(filter-out $(addsuffix .d,$(omit-deps)),\
				      $(+depfiles)))

ifdef +depfiles
ifneq ($(no_deps),t)
-include $(+depfiles)
endif
endif

# Maximize efficiency by minimizing the number of rules.
.SUFFIXES:	# Clear the suffix list.
# Add the suffixes we use.
.SUFFIXES: .a $(object-suffixes) .S .s .c .h .d

# Generic rule for making directories.
%/:
# mkdir isn't smart enough to strip a trailing /.
	mkdir $(@:%/=%)

# Make sure that object files are not removed
# when they are intermediates between sources and library members.
.PRECIOUS: $(addprefix $(objpfx)%,$(object-suffixes))

# Make sure that the parent library archive is never removed.
.PRECIOUS: $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))

# Use the verbose option of ar and tar when not running silently.
ifeq	"$(findstring s,$(MAKEFLAGS))" ""	# if not -s
verbose := v
else	   					# -s
verbose	:=
endif						# not -s

ARFLAGS := r$(verbose)

# This makes all the object files in the parent library archive.

.PHONY: lib libobjs lib-noranlib
lib: lib-noranlib $(foreach l,$(libtypes),$(patsubst %,$(common-objpfx)$l,c))

# Pattern rule for libraries: depend on the __.SYMDEF member updated by ranlib.
lib%.a: lib%.a($(ar-symtab-name)) ;

libobjs: $(foreach o,$(object-suffixes),\
		   $(common-objpfx)$(patsubst %,$(libtype$o),c)(\
		   $(notdir $(objects:.o=$o))))
lib-noranlib: libobjs
others: $(addprefix $(objpfx),$(install-lib))

ifdef objects

# Define a pattern rule that will match many targets libc.a(foo.%), for
# each foo.o in $(objects) (% will just happen always to match `o').  This is
# the only way to define a rule that updates many targets at once with one
# sequence of commands.  We in fact define the pattern rule to match
# targets libc.a(foo.%), libc_pic.a(foo.%), etc, to avoid repeating the rule.
# Each match will only ever be called upon to make member objects of
# the appropriate type in each library (i.e. libc_pic.a(foo.so)).
$(foreach l,$(libtypes),\
	  $(common-objpfx)$(patsubst %,$l,c)($(notdir $(objects:.o=.%)))): \
  $(objpfx)stamp.%-$(subdir) ;

# The pattern rule tells Make to remake $(objpfx)stamp.%-$(subdir) as
# the way to update all the foo.% object files in $(objects).
# Now we define a static pattern rule to update each
# $(objpfx)stamp.SUFFIX-$(subdir) timestamp file;
# these rules (one explicit rule is generated for each object suffix)
# will update the parent archive with
$(foreach o,$(object-suffixes),$(objpfx)stamp$o-$(subdir)): \
  $(objpfx)stamp%-$(subdir): $(objects:.o=%)
	$(patsubst %,cd %;,$(objdir)) \
	$(AR) cru$(verbose) ${O%-lib} \
	      $(patsubst $(objpfx)%,%,$^)
# `touch' won't let me touch a world-writable file I don't own.  Sigh.
	rm -f $@
	touch $@
ifdef subdir
O%-lib = $(filter ../,$(firstword $(objdir) ../))$(patsubst %,$(libtype$*),c)
else
O%-lib = $(patsubst %,$(libtype$*),c)
endif

endif

# Rules to update the $(ar-symtab-name) member with ranlib,
# one for each object flavor.
define o-iterator-doit
$(common-objpfx)$(patsubst %,$(libtype$o),c)($(ar-symtab-name)): \
	  $(common-objpfx)$(patsubst %,$(libtype$o),c)(\
	  $(patsubst $(objpfx)%.o,%$o,$(objects))) \
	  $(filter subdir_lib,$(firstword $(subdir) subdir_lib)); \
	$$(RANLIB) $$(common-objpfx)$$(patsubst %,$$(libtype$o),c)
endef
object-suffixes-left = $(object-suffixes)
include $(o-iterator)


# This makes all the object files.
.PHONY: objects objs
objects objs: $(foreach o,$(object-suffixes),$(objects:.o=$o)) \
	      $(addprefix $(objpfx),$(extra-objs))

# Canned sequence for building an extra library archive.
define build-extra-lib
$(patsubst %,cd %;,$(objdir)) \
$(AR) cru$(verbose) $(@:$(objpfx)%=%) \
      $(patsubst $(objpfx)%,%,$^)
$(RANLIB) $@
endef

# Installation.

# $(install-lib) are installed from the object directory into $(libdir);
# files in $(install-lib) matching `lib%.a' are ranlib'd after installation
# unless they also appear in $(non-lib.a).  $(install-data) are installed
# as they are into $(datadir).  $(headers) are installed as they are in
# $(includedir).  $(install-bin) and $(install-sbin) are installed from the
# object directory into $(bindir) and $(sbindir), respectively.
# $(install-others) are absolute path names of files to install; rules to
# install them are defined elsewhere.

# The simple library name to install libc.a under.
# This could be defined by a sysdep Makefile.
ifndef libc-name
libc-name := c
endif

define do-install
$(make-target-directory)
$(INSTALL_DATA) $< $@
endef

# Make the target directory if it doesn't exist.  Because of make's
# directory cache, this will produce the `mkdir' command when the directory
# already exists, if it didn't exist at the start of the `make' run.  The
# `-' prefix ignores errors from mkdir, so there will just be some
# gratuitous but harmless `File exists' messages.
define make-target-directory
$(addprefix -mkdir ,$(filter-out $(wildcard $(@D:%/=%)),$(@D:%/=%)))
endef

# Any directory (parent or subdir) that has any object files to build
# should install libc.a; this way "make install" in a subdir is guaranteed
# to install everything it changes.
ifdef objects
installed-libcs := $(foreach o,$(object-suffixes),\
			     $(libdir)/$(patsubst %,$(libtype$o),\
						  $(libprefix)$(libc-name)))
installed-libcs := $(filter-out %_pic.a,$(installed-libcs))
install: $(installed-libcs)
$(installed-libcs): $(libdir)/lib$(libprefix)%: lib
	$(make-target-directory)
	$(INSTALL_DATA) $(common-objpfx)lib$(*:$(libc-name)%=c%) $@
# Running ranlib after installing makes the __.SYMDEF time stamp up to
# date, which avoids messages from some linkers.
	$(RANLIB) $@
endif

define do-install-program
$(make-target-directory)
$(INSTALL_PROGRAM) $< $@.new
mv -f $@.new $@
endef

install-lib.so := $(filter %.so,$(install-lib:%_pic.a=%.so))
install-lib := $(filter-out %.so %_pic.a,$(install-lib))
ifeq (yes,$(build-shared))
install-lib-nosubdir: $(foreach so,$(install-lib.so),\
  $(libdir)/$(patsubst $(libprefix)lib%,lib$(libprefix)%,\
		       $(libprefix)$(so))$($(so)-version))

install: $(slibdir)/libc.so$(libc.so-version)
$(slibdir)/lib$(libprefix)c.so$(libc.so-version): $(common-objpfx)libc.so
	$(do-install-so)

define do-install-so
$(do-install-program)
$(patsubst %,ln -s -f $(@F) $(@D)/$(patsubst %$*.so,%,$(<F))$(libprefix)$*.so,\
	   $(filter-out %.so,$@))
endef

so-versions := $(sort $(foreach so,$(install-lib.so),.so$($(so)-version)))
$(foreach v,$(so-versions),\
	  $(libdir)/lib$(libprefix)%$v): $(common-objpfx)lib%.so
	$(do-install-so)
$(foreach v,$(so-versions),\
	  $(libdir)/$(libprefix)%$v): $(common-objpfx)%.so
	$(do-install-so)
endif

ifdef install-bin
$(addprefix $(bindir)/,$(install-bin)): $(bindir)/%: $(objpfx)%
	$(do-install-program)
endif
ifdef install-sbin
$(addprefix $(sbindir)/,$(install-sbin)): $(sbindir)/%: $(objpfx)%
	$(do-install-program)
endif
ifdef install-lib
install-lib.a := $(filter lib%.a,$(install-lib))
install-lib-non.a := $(filter-out lib%.a,$(install-lib))
ifdef install-lib-non.a
$(addprefix $(libdir)/$(libprefix),$(install-lib-non.a)): \
  $(libdir)/$(libprefix)%: $(objpfx)%
	$(do-install)
endif
ifdef install-lib.a
$(install-lib.a:lib%.a=$(libdir)/lib$(libprefix)%.a): \
  $(libdir)/lib$(libprefix)%.a: $(objpfx)lib%.a
	$(do-install)
	$(patsubst %,$(RANLIB) $@,$(filter-out $(non-lib.a),$(<F)))
endif
endif
ifdef install-data
$(addprefix $(datadir)/,$(install-data)): $(datadir)/%: %;$(do-install)
endif
headers := $(strip $(headers))
ifdef headers
$(addprefix $(includedir)/,$(headers)): \
	$(includedir)/%: %;$(do-install)
endif	# headers

.PHONY: install-bin-nosubdir install-sbin-nosubdir install-lib-nosubdir \
	install-data-nosubdir install-headers-nosubdir
install-bin-nosubdir: $(addprefix $(bindir)/,$(install-bin))
install-sbin-nosubdir: $(addprefix $(sbindir)/,$(install-sbin))
install-lib-nosubdir: $(addprefix $(libdir)/,\
		       $(patsubst lib%.a,lib$(libprefix)%.a,$(install-lib.a)) \
		       $(addprefix $(libprefix),$(install-lib-non.a)))
install-data-nosubdir: $(addprefix $(datadir)/,$(install-data))
install-headers-nosubdir: $(addprefix $(includedir)/,$(headers))
install-others-nosubdir: $(install-others)

# We need all the `-nosubdir' targets so that `install' in the parent
# doesn't depend on several things which each iterate over the subdirs.
# This rule makes `install-FOO' always use `install-FOO-nosubdir' as a
# subroutine.  Then in the parent `install-FOO' also causes subdir makes.
install-%:: install-%-nosubdir ;

.PHONY: install install-no-libc.a-nosubdir
install-no-libc.a-nosubdir: install-headers-nosubdir install-data-nosubdir\
			    install-bin-nosubdir install-lib-nosubdir	\
			    install-others-nosubdir install-sbin-nosubdir
install: install-no-libc.a-nosubdir

ifeq (yes,$(build-shared))
# Pattern rule to build a shared object from an archive of PIC objects.
# This must come after the installation rules so Make doesn't try to
# build shared libraries in place from the installed *_pic.a files.
# $(LDLIBS-%.so) may contain -l switches to generate run-time dependencies
# on other shared objects.
lib%.so: lib%_pic.a; $(build-shlib)

define build-shlib
$(LINK.o) -shared -o $@ $(sysdep-LDFLAGS) $(config-LDFLAGS)  \
	  -Wl,-soname=lib$(libprefix)$(@F:lib%.so=%).so$($(@F)-version) \
	  $(LDFLAGS.so) $(LDFLAGS-$(@F:lib%.so=%).so) \
	  -Wl,-rpath-link=$(common-objdir) -Wl,-rpath=$(libdir) \
	  -Wl,--whole-archive $^ $(LDLIBS-$(@F:lib%.so=%).so)
endef

# Don't try to use -lc when making libc.so itself.
# Also omits crti.o and crtn.o, which we do not want
# since we define our own `.init' section specially.
LDFLAGS-c.so = -nostdlib -nostartfiles
# Give libc.so an entry point and make it directly runnable itself.
LDFLAGS-c.so += -e __libc_print_version
# Use our own special initializer and finalizer files for libc.so.
elfobjdir := $(firstword $(objdir) $(..)elf)
$(common-objpfx)libc.so: $(elfobjdir)/soinit.so \
			 $(common-objpfx)libc_pic.a \
			 $(elfobjdir)/sofini.so
	$(build-shlib)

ifdef libc.so-version
$(common-objpfx)libc.so$(libc.so-version): $(common-objpfx)libc.so
	rm -f $@
	ln -s $(<F) $@ || ln $< $@
endif
endif


# Command to compile $< in $(objdir) using the native libraries.
define native-compile
cwd=`pwd`; cd $(@D); $(BUILD_CC) $(BUILD_CFLAGS) \
	   $(addprefix $$cwd/,$^) -o $(@F)
endef

# Command to compile $< in $(common-objdir) using the native libraries.
define common-objdir-compile
cd $(@D); $(BUILD_CC) $(BUILD_CFLAGS) $(<:$(common-objpfx)%=%) -o $(@F)
endef

# We always want to use configuration definitions.
BUILD_CFLAGS = -include $(common-objpfx)config.h

# Support the GNU standard name for this target.
.PHONY: check
check: tests

.PHONY: TAGS
TAGS: distinfo $(..)MakeTAGS
	$(MAKE) $(addprefix -f ,$^) $@

.PHONY: echo-headers
echo-headers:
	@echo $(headers)


# Common cleaning targets.

.PHONY: common-mostlyclean common-clean mostlyclean clean
clean: common-clean
mostlyclean: common-mostlyclean

# Remove the object files.
common-mostlyclean:
	-rm -f $(addprefix $(objpfx),$(tests) $(others) \
				     $(addsuffix .o,$(tests) $(others)) \
				     $(addsuffix .out,$(tests)))
	-rm -f $(addprefix $(objpfx),$(extra-objs) $(install-lib))
	-rm -f core $(objpfx)stub-$(subdir)
	$(rmobjs)
define rmobjs
$(foreach o,$(object-suffixes),
-rm -f $(addprefix $(objpfx),stamp$o-$(subdir)) $(objects:.o=$o))
endef

# Also remove the dependencies and generated source files.
common-clean: common-mostlyclean
	-rm -f $(addprefix $(objpfx),$(generated)) $(+depfiles)

# Produce a file `stub-$(subdir)' which contains `#define __stub_FUNCTION'
# for each function which is a stub.  We grovel over all the .d files
# looking for references to source files in sysdeps/stub.  Then we grovel
# over each referenced source file to see what stub function it defines.

.PHONY: stubs # The parent Makefile calls this target.
stubs: $(common-objpfx)stub-$(subdir)
s = $(sysdep_dir)/stub
$(common-objpfx)stub-$(subdir): $(+depfiles)
# Use /dev/null since `...` might expand to empty.
	s=`cd $s; /bin/pwd`; \
	$(patsubst %,cd %;,$(objdir)) \
	sed -n 's/^stub_warning *(\([^)]*\).*$$/#define __stub_\1/p' \
	    `sed -n 's@^.*$s/\([a-z0-9_-]*\.c\).*$$@'"$$s"/'\1@p' \
		 $(patsubst $(objpfx)%,%,$^) /dev/null` \
	    /dev/null > $@T
	mv -f $@T $@

# Make the distribution tar file.

.PHONY: dist
dist: distinfo $(..)Make-dist
	$(MAKE) -f $< -f $(word 2,$^) $(Make-dist-args)

# Avoid depending on source files found in sysdeps dirs,
# because the references affect implicit rule selection.
dist: $(filter-out %.c %.S %.s,$(distribute))

# We used to simply export all these variables, but that frequently made the
# environment get too large.  Instead, we write all the information into
# a generated makefile fragment `distinfo', and then include it with -f in
# the sub-make that makes the distribution (above).
distinfo: Makefile $(..)Makerules
	$(distinfo-vars)
	mv -f $@.new $@

define distinfo-vars
rm -f $@.new
$(foreach var,subdir subdir-dirs sources elided-routines headers distribute \
	      dont_distribute generated others tests,
echo >> $@.new '$(var)	:= $($(var))')
echo >> $@.new 'sources := $$(sources) $$(addsuffix .c,$$(elided-routines))'
endef

ifneq (,$(strip $(gpl2lgpl)))
ifneq (,$(wildcard $(..)gpl2lgpl.sed))
# Snarf from the master source and frob the copying notice.
$(gpl2lgpl): %: $(..)gpl2lgpl.sed /home/gd/gnu/lib/%
	sed -f $^ > $@-tmp
# So I don't edit them by mistake.
	chmod a-w $@-tmp
	mv -f $@-tmp $@
	test ! -d CVS || cvs commit -m'Updated from $^' $@
endif
endif
class="hl opt">, transfer, sig); } void frv_sim_engine_halt_hook (SIM_DESC sd, SIM_CPU *current_cpu, sim_cia cia) { int i; if (current_cpu != NULL) CIA_SET (current_cpu, cia); /* Invalidate the insn and data caches of all cpus. */ for (i = 0; i < MAX_NR_PROCESSORS; ++i) { current_cpu = STATE_CPU (sd, i); frv_cache_invalidate_all (CPU_INSN_CACHE (current_cpu), 0); frv_cache_invalidate_all (CPU_DATA_CACHE (current_cpu), 1); } frv_term (sd); } /* Read/write functions for system call interface. */ static int syscall_read_mem (host_callback *cb, struct cb_syscall *sc, unsigned long taddr, char *buf, int bytes) { SIM_DESC sd = (SIM_DESC) sc->p1; SIM_CPU *cpu = (SIM_CPU *) sc->p2; frv_cache_invalidate_all (CPU_DATA_CACHE (cpu), 1); return sim_core_read_buffer (sd, cpu, read_map, buf, taddr, bytes); } static int syscall_write_mem (host_callback *cb, struct cb_syscall *sc, unsigned long taddr, const char *buf, int bytes) { SIM_DESC sd = (SIM_DESC) sc->p1; SIM_CPU *cpu = (SIM_CPU *) sc->p2; frv_cache_invalidate_all (CPU_INSN_CACHE (cpu), 0); frv_cache_invalidate_all (CPU_DATA_CACHE (cpu), 1); return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes); } /* Handle TRA and TIRA insns. */ void frv_itrap (SIM_CPU *current_cpu, PCADDR pc, USI base, SI offset) { SIM_DESC sd = CPU_STATE (current_cpu); host_callback *cb = STATE_CALLBACK (sd); USI num = ((base + offset) & 0x7f) + 0x80; #ifdef SIM_HAVE_BREAKPOINTS /* Check for breakpoints "owned" by the simulator first, regardless of --environment. */ if (num == TRAP_BREAKPOINT) { /* First try sim-break.c. If it's a breakpoint the simulator "owns" it doesn't return. Otherwise it returns and let's us try. */ sim_handle_breakpoint (sd, current_cpu, pc); /* Fall through. */ } #endif if (STATE_ENVIRONMENT (sd) == OPERATING_ENVIRONMENT) { frv_queue_software_interrupt (current_cpu, num); return; } switch (num) { case TRAP_SYSCALL : { CB_SYSCALL s; CB_SYSCALL_INIT (&s); s.func = GET_H_GR (7); s.arg1 = GET_H_GR (8); s.arg2 = GET_H_GR (9); s.arg3 = GET_H_GR (10); if (s.func == TARGET_SYS_exit) { sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1); } s.p1 = (PTR) sd; s.p2 = (PTR) current_cpu; s.read_mem = syscall_read_mem; s.write_mem = syscall_write_mem; cb_syscall (cb, &s); SET_H_GR (8, s.result); SET_H_GR (9, s.result2); SET_H_GR (10, s.errcode); break; } case TRAP_BREAKPOINT: sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP); break; /* Add support for dumping registers, either at fixed traps, or all unknown traps if configured with --enable-sim-trapdump. */ default: #if !TRAPDUMP frv_queue_software_interrupt (current_cpu, num); return; #endif #ifdef TRAP_REGDUMP1 case TRAP_REGDUMP1: #endif #ifdef TRAP_REGDUMP2 case TRAP_REGDUMP2: #endif #if TRAPDUMP || (defined (TRAP_REGDUMP1)) || (defined (TRAP_REGDUMP2)) { char buf[256]; int i, j; buf[0] = 0; if (STATE_TEXT_SECTION (sd) && pc >= STATE_TEXT_START (sd) && pc < STATE_TEXT_END (sd)) { const char *pc_filename = (const char *)0; const char *pc_function = (const char *)0; unsigned int pc_linenum = 0; if (bfd_find_nearest_line (STATE_PROG_BFD (sd), STATE_TEXT_SECTION (sd), (struct bfd_symbol **) 0, pc - STATE_TEXT_START (sd), &pc_filename, &pc_function, &pc_linenum) && (pc_function || pc_filename)) { char *p = buf+2; buf[0] = ' '; buf[1] = '('; if (pc_function) { strcpy (p, pc_function); p += strlen (p); } else { char *q = (char *) strrchr (pc_filename, '/'); strcpy (p, (q) ? q+1 : pc_filename); p += strlen (p); } if (pc_linenum) { sprintf (p, " line %d", pc_linenum); p += strlen (p); } p[0] = ')'; p[1] = '\0'; if ((p+1) - buf > sizeof (buf)) abort (); } } sim_io_printf (sd, "\nRegister dump, pc = 0x%.8x%s, base = %u, offset = %d\n", (unsigned)pc, buf, (unsigned)base, (int)offset); for (i = 0; i < 64; i += 8) { long g0 = (long)GET_H_GR (i); long g1 = (long)GET_H_GR (i+1); long g2 = (long)GET_H_GR (i+2); long g3 = (long)GET_H_GR (i+3); long g4 = (long)GET_H_GR (i+4); long g5 = (long)GET_H_GR (i+5); long g6 = (long)GET_H_GR (i+6); long g7 = (long)GET_H_GR (i+7); if ((g0 | g1 | g2 | g3 | g4 | g5 | g6 | g7) != 0) sim_io_printf (sd, "\tgr%02d - gr%02d: 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx\n", i, i+7, g0, g1, g2, g3, g4, g5, g6, g7); } for (i = 0; i < 64; i += 8) { long f0 = (long)GET_H_FR (i); long f1 = (long)GET_H_FR (i+1); long f2 = (long)GET_H_FR (i+2); long f3 = (long)GET_H_FR (i+3); long f4 = (long)GET_H_FR (i+4); long f5 = (long)GET_H_FR (i+5); long f6 = (long)GET_H_FR (i+6); long f7 = (long)GET_H_FR (i+7); if ((f0 | f1 | f2 | f3 | f4 | f5 | f6 | f7) != 0) sim_io_printf (sd, "\tfr%02d - fr%02d: 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx\n", i, i+7, f0, f1, f2, f3, f4, f5, f6, f7); } sim_io_printf (sd, "\tlr/lcr/cc/ccc: 0x%.8lx 0x%.8lx 0x%.8lx 0x%.8lx\n", (long)GET_H_SPR (272), (long)GET_H_SPR (273), (long)GET_H_SPR (256), (long)GET_H_SPR (263)); } break; #endif } } /* Handle the MTRAP insn. */ void frv_mtrap (SIM_CPU *current_cpu) { SIM_DESC sd = CPU_STATE (current_cpu); /* Check the status of media exceptions in MSR0. */ SI msr = GET_MSR (0); if (GET_MSR_AOVF (msr) || GET_MSR_MTT (msr) && STATE_ARCHITECTURE (sd)->mach != bfd_mach_fr550) frv_queue_program_interrupt (current_cpu, FRV_MP_EXCEPTION); } /* Handle the BREAK insn. */ void frv_break (SIM_CPU *current_cpu) { IADDR pc; SIM_DESC sd = CPU_STATE (current_cpu); #ifdef SIM_HAVE_BREAKPOINTS /* First try sim-break.c. If it's a breakpoint the simulator "owns" it doesn't return. Otherwise it returns and let's us try. */ pc = GET_H_PC (); sim_handle_breakpoint (sd, current_cpu, pc); /* Fall through. */ #endif if (STATE_ENVIRONMENT (sd) != OPERATING_ENVIRONMENT) { /* Invalidate the insn cache because the debugger will presumably replace the breakpoint insn with the real one. */ #ifndef SIM_HAVE_BREAKPOINTS pc = GET_H_PC (); #endif sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP); } frv_queue_break_interrupt (current_cpu); } /* Return from trap. */ USI frv_rett (SIM_CPU *current_cpu, PCADDR pc, BI debug_field) { USI new_pc; /* if (normal running mode and debug_field==0 PC=PCSR PSR.ET=1 PSR.S=PSR.PS else if (debug running mode and debug_field==1) PC=(BPCSR) PSR.ET=BPSR.BET PSR.S=BPSR.BS change to normal running mode */ int psr_s = GET_H_PSR_S (); int psr_et = GET_H_PSR_ET (); /* Check for exceptions in the priority order listed in the FRV Architecture Volume 2. */ if (! psr_s) { /* Halt if PSR.ET is not set. See chapter 6 of the LSI. */ if (! psr_et) { SIM_DESC sd = CPU_STATE (current_cpu); sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP); } /* privileged_instruction interrupt will have already been queued by frv_detect_insn_access_interrupts. */ new_pc = pc + 4; } else if (psr_et) { /* Halt if PSR.S is set. See chapter 6 of the LSI. */ if (psr_s) { SIM_DESC sd = CPU_STATE (current_cpu); sim_engine_halt (sd, current_cpu, NULL, pc, sim_stopped, SIM_SIGTRAP); } frv_queue_program_interrupt (current_cpu, FRV_ILLEGAL_INSTRUCTION); new_pc = pc + 4; } else if (! CPU_DEBUG_STATE (current_cpu) && debug_field == 0) { USI psr = GET_PSR (); /* Return from normal running state. */ new_pc = GET_H_SPR (H_SPR_PCSR); SET_PSR_ET (psr, 1); SET_PSR_S (psr, GET_PSR_PS (psr)); sim_queue_fn_si_write (current_cpu, frvbf_h_spr_set, H_SPR_PSR, psr); } else if (CPU_DEBUG_STATE (current_cpu) && debug_field == 1) { USI psr = GET_PSR (); /* Return from debug state. */ new_pc = GET_H_SPR (H_SPR_BPCSR); SET_PSR_ET (psr, GET_H_BPSR_BET ()); SET_PSR_S (psr, GET_H_BPSR_BS ()); sim_queue_fn_si_write (current_cpu, frvbf_h_spr_set, H_SPR_PSR, psr); CPU_DEBUG_STATE (current_cpu) = 0; } else new_pc = pc + 4; return new_pc; } /* Functions for handling non-excepting instruction side effects. */ static SI next_available_nesr (SIM_CPU *current_cpu, SI current_index) { FRV_REGISTER_CONTROL *control = CPU_REGISTER_CONTROL (current_cpu); if (control->spr[H_SPR_NECR].implemented) { int limit; USI necr = GET_NECR (); /* See if any NESRs are implemented. First need to check the validity of the NECR. */ if (! GET_NECR_VALID (necr)) return NO_NESR; limit = GET_NECR_NEN (necr); for (++current_index; current_index < limit; ++current_index) { SI nesr = GET_NESR (current_index); if (! GET_NESR_VALID (nesr)) return current_index; } } return NO_NESR; } static SI next_valid_nesr (SIM_CPU *current_cpu, SI current_index) { FRV_REGISTER_CONTROL *control = CPU_REGISTER_CONTROL (current_cpu); if (control->spr[H_SPR_NECR].implemented) { int limit; USI necr = GET_NECR (); /* See if any NESRs are implemented. First need to check the validity of the NECR. */ if (! GET_NECR_VALID (necr)) return NO_NESR; limit = GET_NECR_NEN (necr); for (++current_index; current_index < limit; ++current_index) { SI nesr = GET_NESR (current_index); if (GET_NESR_VALID (nesr)) return current_index; } } return NO_NESR; } BI frvbf_check_non_excepting_load ( SIM_CPU *current_cpu, SI base_index, SI disp_index, SI target_index, SI immediate_disp, QI data_size, BI is_float ) { BI rc = 1; /* perform the load. */ SIM_DESC sd = CPU_STATE (current_cpu); int daec = 0; int rec = 0; int ec = 0; USI necr; int do_elos; SI NE_flags[2]; SI NE_base; SI nesr; SI ne_index; FRV_REGISTER_CONTROL *control; SI address = GET_H_GR (base_index); if (disp_index >= 0) address += GET_H_GR (disp_index); else address += immediate_disp; /* Check for interrupt factors. */ switch (data_size) { case NESR_UQI_SIZE: case NESR_QI_SIZE: break; case NESR_UHI_SIZE: case NESR_HI_SIZE: if (address & 1) ec = 1; break; case NESR_SI_SIZE: if (address & 3) ec = 1; break; case NESR_DI_SIZE: if (address & 7) ec = 1; if (target_index & 1) rec = 1; break; case NESR_XI_SIZE: if (address & 0xf) ec = 1; if (target_index & 3) rec = 1; break; default: { IADDR pc = GET_H_PC (); sim_engine_abort (sd, current_cpu, pc, "check_non_excepting_load: Incorrect data_size\n"); break; } } control = CPU_REGISTER_CONTROL (current_cpu); if (control->spr[H_SPR_NECR].implemented) { necr = GET_NECR (); do_elos = GET_NECR_VALID (necr) && GET_NECR_ELOS (necr); } else do_elos = 0; /* NECR, NESR, NEEAR are only implemented for the full frv machine. */ if (do_elos) { ne_index = next_available_nesr (current_cpu, NO_NESR); if (ne_index == NO_NESR) { IADDR pc = GET_H_PC (); sim_engine_abort (sd, current_cpu, pc, "No available NESR register\n"); } /* Fill in the basic fields of the NESR. */ nesr = GET_NESR (ne_index); SET_NESR_VALID (nesr); SET_NESR_EAV (nesr); SET_NESR_DRN (nesr, target_index); SET_NESR_SIZE (nesr, data_size); SET_NESR_NEAN (nesr, ne_index); if (is_float) SET_NESR_FR (nesr); else CLEAR_NESR_FR (nesr); /* Set the corresponding NEEAR. */ SET_NEEAR (ne_index, address); SET_NESR_DAEC (nesr, 0); SET_NESR_REC (nesr, 0); SET_NESR_EC (nesr, 0); } /* Set the NE flag corresponding to the target register if an interrupt factor was detected. daec is not checked here yet, but is declared for future reference. */ if (is_float) NE_base = H_SPR_FNER0; else NE_base = H_SPR_GNER0; GET_NE_FLAGS (NE_flags, NE_base); if (rec) { SET_NE_FLAG (NE_flags, target_index); if (do_elos) SET_NESR_REC (nesr, NESR_REGISTER_NOT_ALIGNED); } if (ec) { SET_NE_FLAG (NE_flags, target_index); if (do_elos) SET_NESR_EC (nesr, NESR_MEM_ADDRESS_NOT_ALIGNED); } if (do_elos) SET_NESR (ne_index, nesr); /* If no interrupt factor was detected then set the NE flag on the target register if the NE flag on one of the input registers is already set. */ if (! rec && ! ec && ! daec) { BI ne_flag = GET_NE_FLAG (NE_flags, base_index); if (disp_index >= 0) ne_flag |= GET_NE_FLAG (NE_flags, disp_index); if (ne_flag) { SET_NE_FLAG (NE_flags, target_index); rc = 0; /* Do not perform the load. */ } else CLEAR_NE_FLAG (NE_flags, target_index); } SET_NE_FLAGS (NE_base, NE_flags); return rc; /* perform the load? */ } /* Record state for media exception: media_cr_not_aligned. */ void frvbf_media_cr_not_aligned (SIM_CPU *current_cpu) { SIM_DESC sd = CPU_STATE (current_cpu); /* On some machines this generates an illegal_instruction interrupt. */ switch (STATE_ARCHITECTURE (sd)->mach) { /* Note: there is a discrepancy between V2.2 of the FR400 instruction manual and the various FR4xx LSI specs. The former claims that unaligned registers cause an mp_exception while the latter say it's an illegal_instruction. The LSI specs appear to be correct since MTT is fixed at 1. */ case bfd_mach_fr400: case bfd_mach_fr450: case bfd_mach_fr550: frv_queue_program_interrupt (current_cpu, FRV_ILLEGAL_INSTRUCTION); break; default: frv_set_mp_exception_registers (current_cpu, MTT_CR_NOT_ALIGNED, 0); break; } } /* Record state for media exception: media_acc_not_aligned. */ void frvbf_media_acc_not_aligned (SIM_CPU *current_cpu) { SIM_DESC sd = CPU_STATE (current_cpu); /* On some machines this generates an illegal_instruction interrupt. */ switch (STATE_ARCHITECTURE (sd)->mach) { /* See comment in frvbf_cr_not_aligned(). */ case bfd_mach_fr400: case bfd_mach_fr450: case bfd_mach_fr550: frv_queue_program_interrupt (current_cpu, FRV_ILLEGAL_INSTRUCTION); break; default: frv_set_mp_exception_registers (current_cpu, MTT_ACC_NOT_ALIGNED, 0); break; } } /* Record state for media exception: media_register_not_aligned. */ void frvbf_media_register_not_aligned (SIM_CPU *current_cpu) { SIM_DESC sd = CPU_STATE (current_cpu); /* On some machines this generates an illegal_instruction interrupt. */ switch (STATE_ARCHITECTURE (sd)->mach) { /* See comment in frvbf_cr_not_aligned(). */ case bfd_mach_fr400: case bfd_mach_fr450: case bfd_mach_fr550: frv_queue_program_interrupt (current_cpu, FRV_ILLEGAL_INSTRUCTION); break; default: frv_set_mp_exception_registers (current_cpu, MTT_INVALID_FR, 0); break; } } /* Record state for media exception: media_overflow. */ void frvbf_media_overflow (SIM_CPU *current_cpu, int sie) { frv_set_mp_exception_registers (current_cpu, MTT_OVERFLOW, sie); } /* Queue a division exception. */ enum frv_dtt frvbf_division_exception (SIM_CPU *current_cpu, enum frv_dtt dtt, int target_index, int non_excepting) { /* If there was an overflow and it is masked, then record it in ISR.AEXC. */ USI isr = GET_ISR (); if ((dtt & FRV_DTT_OVERFLOW) && GET_ISR_EDE (isr)) { dtt &= ~FRV_DTT_OVERFLOW; SET_ISR_AEXC (isr); SET_ISR (isr); } if (dtt != FRV_DTT_NO_EXCEPTION) { if (non_excepting) { /* Non excepting instruction, simply set the NE flag for the target register. */ SI NE_flags[2]; GET_NE_FLAGS (NE_flags, H_SPR_GNER0); SET_NE_FLAG (NE_flags, target_index); SET_NE_FLAGS (H_SPR_GNER0, NE_flags); } else frv_queue_division_exception_interrupt (current_cpu, dtt); } return dtt; } void frvbf_check_recovering_store ( SIM_CPU *current_cpu, PCADDR address, SI regno, int size, int is_float ) { FRV_CACHE *cache = CPU_DATA_CACHE (current_cpu); int reg_ix; CPU_RSTR_INVALIDATE(current_cpu) = 0; for (reg_ix = next_valid_nesr (current_cpu, NO_NESR); reg_ix != NO_NESR; reg_ix = next_valid_nesr (current_cpu, reg_ix)) { if (address == GET_H_SPR (H_SPR_NEEAR0 + reg_ix)) { SI nesr = GET_NESR (reg_ix); int nesr_drn = GET_NESR_DRN (nesr); BI nesr_fr = GET_NESR_FR (nesr); SI remain; /* Invalidate cache block containing this address. If we need to count cycles, then the cache operation will be initiated from the model profiling functions. See frvbf_model_.... */ if (model_insn) { CPU_RSTR_INVALIDATE(current_cpu) = 1; CPU_LOAD_ADDRESS (current_cpu) = address; } else frv_cache_invalidate (cache, address, 1/* flush */); /* Copy the stored value to the register indicated by NESR.DRN. */ for (remain = size; remain > 0; remain -= 4) { SI value; if (is_float) value = GET_H_FR (regno); else value = GET_H_GR (regno); switch (size) { case 1: value &= 0xff; break; case 2: value &= 0xffff; break; default: break; } if (nesr_fr) sim_queue_fn_sf_write (current_cpu, frvbf_h_fr_set, nesr_drn, value); else sim_queue_fn_si_write (current_cpu, frvbf_h_gr_set, nesr_drn, value); nesr_drn++; regno++; } break; /* Only consider the first matching register. */ } } /* loop over active neear registers. */ } SI frvbf_check_acc_range (SIM_CPU *current_cpu, SI regno) { /* Only applicable to fr550 */ SIM_DESC sd = CPU_STATE (current_cpu); if (STATE_ARCHITECTURE (sd)->mach != bfd_mach_fr550) return; /* On the fr550, media insns in slots 0 and 2 can only access accumulators acc0-acc3. Insns in slots 1 and 3 can only access accumulators acc4-acc7 */ switch (frv_current_fm_slot) { case UNIT_FM0: case UNIT_FM2: if (regno <= 3) return 1; /* all is ok */ break; case UNIT_FM1: case UNIT_FM3: if (regno >= 4) return 1; /* all is ok */ break; } /* The specified accumulator is out of range. Queue an illegal_instruction interrupt. */ frv_queue_program_interrupt (current_cpu, FRV_ILLEGAL_INSTRUCTION); return 0; } void frvbf_check_swap_address (SIM_CPU *current_cpu, SI address) { /* Only applicable to fr550 */ SIM_DESC sd = CPU_STATE (current_cpu); if (STATE_ARCHITECTURE (sd)->mach != bfd_mach_fr550) return; /* Adress must be aligned on a word boundary. */ if (address & 0x3) frv_queue_data_access_exception_interrupt (current_cpu); } static void clear_nesr_neear (SIM_CPU *current_cpu, SI target_index, BI is_float) { int reg_ix; /* Only implemented for full frv. */ SIM_DESC sd = CPU_STATE (current_cpu); if (STATE_ARCHITECTURE (sd)->mach != bfd_mach_frv) return; /* Clear the appropriate NESR and NEEAR registers. */ for (reg_ix = next_valid_nesr (current_cpu, NO_NESR); reg_ix != NO_NESR; reg_ix = next_valid_nesr (current_cpu, reg_ix)) { SI nesr; /* The register is available, now check if it is active. */ nesr = GET_NESR (reg_ix); if (GET_NESR_FR (nesr) == is_float) { if (target_index < 0 || GET_NESR_DRN (nesr) == target_index) { SET_NESR (reg_ix, 0); SET_NEEAR (reg_ix, 0); } } } } static void clear_ne_flags ( SIM_CPU *current_cpu, SI target_index, int hi_available, int lo_available, SI NE_base ) { SI NE_flags[2]; int exception; GET_NE_FLAGS (NE_flags, NE_base); if (target_index >= 0) CLEAR_NE_FLAG (NE_flags, target_index); else { if (lo_available) NE_flags[1] = 0; if (hi_available) NE_flags[0] = 0; } SET_NE_FLAGS (NE_base, NE_flags); } /* Return 1 if the given register is available, 0 otherwise. TARGET_INDEX==-1 means to check for any register available. */ static void which_registers_available ( SIM_CPU *current_cpu, int *hi_available, int *lo_available, int is_float ) { if (is_float) frv_fr_registers_available (current_cpu, hi_available, lo_available); else frv_gr_registers_available (current_cpu, hi_available, lo_available); } void frvbf_clear_ne_flags (SIM_CPU *current_cpu, SI target_index, BI is_float) { int hi_available; int lo_available; int exception; SI NE_base; USI necr; FRV_REGISTER_CONTROL *control; /* Check for availability of the target register(s). */ which_registers_available (current_cpu, & hi_available, & lo_available, is_float); /* Check to make sure that the target register is available. */ if (! frv_check_register_access (current_cpu, target_index, hi_available, lo_available)) return; /* Determine whether we're working with GR or FR registers. */ if (is_float) NE_base = H_SPR_FNER0; else NE_base = H_SPR_GNER0; /* Always clear the appropriate NE flags. */ clear_ne_flags (current_cpu, target_index, hi_available, lo_available, NE_base); /* Clear the appropriate NESR and NEEAR registers. */ control = CPU_REGISTER_CONTROL (current_cpu); if (control->spr[H_SPR_NECR].implemented) { necr = GET_NECR (); if (GET_NECR_VALID (necr) && GET_NECR_ELOS (necr)) clear_nesr_neear (current_cpu, target_index, is_float); } } void frvbf_commit (SIM_CPU *current_cpu, SI target_index, BI is_float) { SI NE_base; SI NE_flags[2]; BI NE_flag; int exception; int hi_available; int lo_available; USI necr; FRV_REGISTER_CONTROL *control; /* Check for availability of the target register(s). */ which_registers_available (current_cpu, & hi_available, & lo_available, is_float); /* Check to make sure that the target register is available. */ if (! frv_check_register_access (current_cpu, target_index, hi_available, lo_available)) return; /* Determine whether we're working with GR or FR registers. */ if (is_float) NE_base = H_SPR_FNER0; else NE_base = H_SPR_GNER0; /* Determine whether a ne exception is pending. */ GET_NE_FLAGS (NE_flags, NE_base); if (target_index >= 0) NE_flag = GET_NE_FLAG (NE_flags, target_index); else { NE_flag = hi_available && NE_flags[0] != 0 || lo_available && NE_flags[1] != 0; } /* Always clear the appropriate NE flags. */ clear_ne_flags (current_cpu, target_index, hi_available, lo_available, NE_base); control = CPU_REGISTER_CONTROL (current_cpu); if (control->spr[H_SPR_NECR].implemented) { necr = GET_NECR (); if (GET_NECR_VALID (necr) && GET_NECR_ELOS (necr) && NE_flag) { /* Clear the appropriate NESR and NEEAR registers. */ clear_nesr_neear (current_cpu, target_index, is_float); frv_queue_program_interrupt (current_cpu, FRV_COMMIT_EXCEPTION); } } } /* Generate the appropriate fp_exception(s) based on the given status code. */ void frvbf_fpu_error (CGEN_FPU* fpu, int status) { struct frv_fp_exception_info fp_info = { FSR_NO_EXCEPTION, FTT_IEEE_754_EXCEPTION }; if (status & (sim_fpu_status_invalid_snan | sim_fpu_status_invalid_qnan | sim_fpu_status_invalid_isi | sim_fpu_status_invalid_idi | sim_fpu_status_invalid_zdz | sim_fpu_status_invalid_imz | sim_fpu_status_invalid_cvi | sim_fpu_status_invalid_cmp | sim_fpu_status_invalid_sqrt)) fp_info.fsr_mask |= FSR_INVALID_OPERATION; if (status & sim_fpu_status_invalid_div0) fp_info.fsr_mask |= FSR_DIVISION_BY_ZERO; if (status & sim_fpu_status_inexact) fp_info.fsr_mask |= FSR_INEXACT; if (status & sim_fpu_status_overflow) fp_info.fsr_mask |= FSR_OVERFLOW; if (status & sim_fpu_status_underflow) fp_info.fsr_mask |= FSR_UNDERFLOW; if (status & sim_fpu_status_denorm) { fp_info.fsr_mask |= FSR_DENORMAL_INPUT; fp_info.ftt = FTT_DENORMAL_INPUT; } if (fp_info.fsr_mask != FSR_NO_EXCEPTION) { SIM_CPU *current_cpu = (SIM_CPU *)fpu->owner; frv_queue_fp_exception_interrupt (current_cpu, & fp_info); } }