File: bitmask.fs

package info (click to toggle)
gforth 0.7.3%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,888 kB
  • ctags: 1,977
  • sloc: ansic: 8,535; sh: 3,666; lisp: 1,778; makefile: 1,011; yacc: 186; sed: 141; lex: 102; awk: 21
file content (95 lines) | stat: -rw-r--r-- 3,072 bytes parent folder | download | duplicates (2)
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
\ bitmask.fs Generic Bitmask compiler          			13aug97jaw

\ Copyright (C) 1998,2000,2003,2007 Free Software Foundation, Inc.

\ This file is part of Gforth.

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

\ This program 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 General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.

\ This is a tool for building up assemblers.
\ In modern CPU's instructions there are often some bitfields that
\ specify a register, an addressing mode, an immediate value.
\ A value in an instruction word might be represented in one bitfield
\ or several bitfields.
\ If you code it yourself, you have to think about the right shifting
\ operators. E.g. if you want to store a 2-bit value at bit position 3
\ you would code: ( value opcode -- opcode ) swap 3 lshift or
\ If the value is stored at bit-position 2-3 and 5-6 it gets more difficult:
\ ( value opcode -- opcode ) swap dup 3 and 2 lshift rot or swap 3 and 5 lshift or
\ This is no fun! This can be created automatically by: "maskinto %bitfield".
\ This compiles some code like above into the current definition.
\ This code has the same stack-effect then our examples.
\ Additional things compiled: A check whether the value could be represented
\ by the bitfield, the area of the bitfield is cleared in the opcode.

\ Code Compliance:
\
\ This is for 32-bit and 64-bit systems and for GForth only.
\ 

\ Revision Log:
\
\ 13aug97 Jens Wilke	Creation

decimal

: ?bitexceed ( u1 u2 -- u1 )
\G if u1 is greater than u2 the value could not be represented in the bitfield
  over u< ABORT" value exceeds bitfield!" ;

: bitset# ( u -- )
\G returns the number of bits set in a cell
  0 swap 64 0 DO dup 1 and IF swap 1+ swap THEN 1 rshift LOOP drop ;

: max/bits ( u -- u2 )
\G returns the highest number that could be represented by u bits
  1 swap lshift 1- ;

Variable mli	\ masked last i
Variable mst	\ masked state

: (maskinto) ( n -- )
  0 mst !
  0 mli !
  [ -1 bitset# ] literal 0
  DO	mst @
	IF	dup 1 and 0=
		IF I mli @ - ?dup 
		   IF  	postpone dup max/bits mli @ lshift
			postpone literal postpone and postpone rot 
			postpone or postpone swap
		   THEN
		   I mli ! 0 mst !
		THEN
	ELSE	dup 1 and
		IF I mli @ - ?dup
		   IF postpone literal postpone lshift THEN
		   I mli ! 1 mst !
		THEN
	THEN
	1 rshift 
  LOOP drop 
  postpone drop ;

: maskinto ( <mask> )
  name s>number drop
  \ compile: clear maskarea
  dup invert 
  postpone literal postpone and postpone swap
  \ compile: make check
  dup bitset# max/bits
  postpone literal postpone ?bitexceed
  (maskinto) ; immediate

\ : test maskinto %110010 ;